详解shellread命令

shell作为一门语言,自然也具有读数据的功能,read就是按行从文件(或标准输入或给定文件描述符)中读取数据的最佳选择。当使用管道、重定向方式组合命令时感觉达不到自己的需求时,不妨考虑下while read line。

成都创新互联是专业的澄迈网站建设公司,澄迈接单;提供网站制作、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行澄迈网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

1、read基本读取

 1 #!/bin/bash
 2 #testing the read command
 3
 4 echo -n "Enter you name:"   #echo -n 让用户直接在后面输入
 5 read name  #输入的多个文本将保存在一个变量中
 6 echo "Hello $name, welcome to my program."                                      
执行:

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

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

 1 #!/bin/bash
 2 #testing the read -p option
 3 read -p "Please enter your age: " age
 4 days=$[ $age * 365 ]
 5 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 (指定多个变量)

 1 #!/bin/bash
 2 # entering multiple variables
 3
 4 read -p "Enter your name:" first last
 5 echo "Checking data for $last, $first"
执行:

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

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

 1 #!/bin/bash
 2 # testing the REPLY environment variable
 3
 4 read -p "Enter a number: "
 5 factorial=1                        
 6 for (( count=1; count$REPLY; count++ ))
 7 do
 8    factorial=$[ $factorial * $count ]   #等号两端不要有空格
 9 done
10 echo "The factorial of $REPLY is $factorial"
执行:

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

5、超时, 等待输入的秒数(read -t)

 1 #!/bin/bash
 2 # timing the data entry
 3
 4 if read -t 5 -p "Please enter your name: " name     #记得加-p参数, 直接在read命令行指定提示符
 5 then
 6     echo "Hello $name, welcome to my script"
 7 else
 8     echo
 9     echo "Sorry, too slow!"
10 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命令对输入的字符判断

 1 #!/bin/bash
 2 # getting just one character of input
 3
 4 read -n1 -p "Do you want to continue [Y/N]? " answer
 5 case $answer in
 6 Y | y) echo
 7        echo "fine, continue on...";;
 8 N | n) echo
 9        echo "OK, goodbye"
10        exit;;
11 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)

 1 #!/bin/bash
 2 # hiding input data from the monitor
 3
 4 read -s -p "Enter your passwd: " pass   #-s 参数使得read读入的字符隐藏
 5 echo
 6 echo "Is your passwd readlly $pass?"
~                                          
执行:

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

8、从文本中读取

 1 #!/bin/bash
 2 # reading data from a file
 3
 4 count=1
 5 cat test | while read line
 6 do
 7    echo "Line $count: $line"
 8    count=$[ $count + 1 ]
 9 done
10 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

网站题目:详解shellread命令
分享地址:http://www.36103.cn/qtweb/news14/19714.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联