1.软文推荐

2.软文推荐

3.软文推荐

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

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

本文来源:www.lxlinux.net/5415.html,若引用不当,请联系修改。

相关文章 8

1

Linux系统安装Elasticsearch具体步骤 3分钟前

Elasticsearch 是一个分布式的免费开源搜索和分析引擎,适用于包括文本、数字、地理空间、结构化和非结构化数据等在内的所有类型的数据,...

2

黄冈网站开发(黄冈信息网站) 4分钟前

目录:1、黄冈网页制作多少钱?2、我想湖北黄冈市办个媒约平台网站怎么办要那些手续!办证吗?3、惠州网站建设公司有哪些?4、黄冈网站...

3

Django+Ajax异步刷新/定时自动刷新实例 5分钟前

AJAX是前端技术的集合,包括JavaScript、XML、HTML、CSS等,下面这篇文章主要给大家介绍了关于Django+Ajax异步刷新/定时自动刷新的相关资料,文中通...

4

tar 命令使用实例 7分钟前

Linux tar 命令是归档或分发文件时的强大武器。GNU tar 归档包可以包含多个文件和目录,还能保留其文件权限,它还支持多种压缩格式。Tar 表...

5

wordpress打开速度慢(wordpress 很慢) 7分钟前

目录:1、WordPress网站后台打开速度慢怎么办2、WordPress为什么在本地访问速度也很慢3、为什麽WordPress访问速度慢?4、wordpress的网站打开速度...

6

Ubuntu系统提速具体方法 9分钟前

你逐渐成为一名经验丰富的Ubuntu用户,可能注意到Ubuntu系统的速度越来越慢。这可能是由于逐渐安装了大量应用程序,或者配置设置对于更...

7

百度云双11(百度云双重压缩包怎么解压) 10分钟前

目录:1、《2019天猫双十一狂欢夜》免费在线观看完整版高清,求百度网盘资源2、求《双十一流量规划体系》全文免费下载百度网盘资源,谢谢...

8

Linux系统安装zookeeper详细步骤 11分钟前

本篇文章良许教程网为大家分享一下Linux系统如何去安装zookeeper,zookeeper是一个开源的分布式协调服务,在大数据集群中起着重用的作用,废...