shell基础教程二十二:read命令的读取

接收键盘或其它文件描述符的输入。read 命令接收标准输入(键盘)的输入,或者其他文件描述符的输入。得到输入后,read 命令将数据放入一个标准变量中。
read 命令格式如下:

read [选项] [变量名]

选项:
-p:“提示信息”:在等待read输入时,输出提示信息;
-t:秒数:read命令会一直等待用户输入,使用此选项可以指定等待时间;
-n:字符数:read命令只接收指定的字符数就会执行;
-s:隐藏输入的数据,适用于机密信息的输入;

更详细的命令说明,请查看《Linux命令:read-单行读取数据》

变量名可以自定义。如果不指定变量名,则会把输入保存到默认变量REPLY中;
如果只提供了一个变量名,则将整个输入行赋予该变量;
如果提供了一个以上的变量名,则输入行分为若干字,一个接一个地赋予各个变量,而命令行上的最后一个变量取得剩余的所有字符;

1. read的基本读取

#!/bin/bash
#testing the read command

echo -n "Enter you name:"   #echo -n 让用户直接在后面输入 
read name  #输入的多个文本将保存在一个变量中
echo "Hello $name, welcome to my program."                                 

执行:

# ./read.sh
Enter you name: wangtao
Hello wangtao, welcome to my program.

2. read -p (直接在read命令行指定提示符)

#!/bin/bash
#testing the read -p option
read -p "Please enter your age: " age
days=$[ $age * 365 ]
echo "That makes you over $days days old!"
执行:

# ./age.sh
Please enter your age: 23
That makes you over 8395 days old!

3. read -p (指定多个变量)

#!/bin/bash
# entering multiple variables

read -p "Enter your name:" first last
echo "Checking data for $last, $first"
执行:

# ./read1.sh
Enter your name: a b
Checking data for b, a

4. read 命令中不指定变量,那么read命令将他收到的任何数据都放在特殊环境变量REPLY

#!/bin/bash
# testing the REPLY environment variable

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"
执行:

./read2.sh
Enter a number: 6
The factorial of 6 is 720

5. read -t (超时,等到输入的描述)

#!/bin/bash
# timing the data entry

if read -t 5 -p "Please enter your name: " name     #记得加-p参数, 直接在read命令行指定提示符
then
    echo "Hello $name, welcome to my script"
else
    echo 
    echo "Sorry, too slow!"
fi
执行:

# ./read3.sh
Please enter your name: 
Sorry, too slow!
# ./read3.sh 
Please enter your name: wang
Hello wang, welcome to my script

6. read命令对输入的字符判断

#!/bin/bash
# getting just one character of input

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   
执行:

# ./read4.sh
Do you want to continue [Y/N]? y
fine, continue on...

./read4.sh
Do you want to continue [Y/N]? n
OK, goodbye

7. read -s(隐藏方式读取)

#!/bin/bash
# hiding input data from the monitor

read -s -p "Enter your passwd: " pass   #-s 参数使得read读入的字符隐藏
echo 
echo "Is your passwd readlly $pass?"
~                                          
执行:

# ./read5.sh
Enter your passwd: 
Is your passwd readlly osfile@206?

8. 从文本中读取

#!/bin/bash
# reading data from a file

count=1
cat test | while read line
do
   echo "Line $count: $line"
   count=$[ $count + 1 ]
done
echo "Finished processing the file"
执行:

 ./read6.sh
Line 1: The quick brown dog jumps over the lazy fox.
Line 2: This is a test, this is only a test.
Line 3: O Romeo, Romeo! Wherefore art thou Romeo?
Finished processing the file
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值