shell 接受键盘输入 read命令

命令

read [options] [var]
KeyValue
-p指定要显示的提示
-s静默输入,一般用于密码
-n #指定输入的字符长度最大值#
-d ‘字符’输入结束符,当你输入的内容出现这个字符时,立即结束输入
-t N超出N秒没有进行输入,则自动退出
#!/bin/bash
echo -n "Enter you name:" 
read name
echo "Hello $name, welcome to my program."
#!/bin/bash
read -p "Please enter your age: " age
days=$[ $age * 365 ]
echo "That makes you over $days days old!"
#!/bin/bash
read -p "Enter your name:" first last
echo "Checking data for $last, $first"
#!/bin/bash
read -p "Enter a number: "
factorial=1                         
for (( count=1; count<= $REPLY; count++ ))
do
	factorial=$[ $factorial * $count ]   #等号两端不要有空格
done
echo "The factorial of $REPLY is $factorial"
#!/bin/bash
if read -t 5 -p "Please enter your name: " name #记得加-p参数, 直接在read命令行指定提示符
then
	echo "Hello $name, welcome to my script"
else
	echo "Sorry, too slow!"
fi
#!/bin/bash
read -n1 -p "Do you want to continue [Y/N]? " answer
case $answer in
Y | y) echo
       echo "fine, continue on...";;
N | n) echo 
       echo "OK, goodbye"
       exit;;
esac
#!/bin/bash
read -s -p "Enter your passwd: " pass   #-s 参数使得read读入的字符隐藏
echo 
echo "Is your passwd readlly $pass?"
#!/bin/bash
count=1
cat test | while read line
do
   echo "Line $count: $line"
   count=$[ $count + 1 ]
done
echo "Finished processing the file"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值