shell 接受键盘输入 read命令

命令

read [options] [var]
Key Value
-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"

猜你喜欢

转载自blog.csdn.net/zxsean/article/details/105358174
今日推荐