(转)Shell中read的用法详解
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(转)Shell中read的用法详解相关的知识,希望对你有一定的参考价值。
Shell中read的用法详解
原文:http://blog.csdn.net/jerry_1126/article/details/77406500
read的常用用法如下:
read -[pstnd] var1 var2 ...
-p 提示语句
-n 字符个数
-s 屏蔽回显
-t 等待时间
-d 输入分界
- 01). read # 从标准输入读取一行并赋值给特定变量REPLY
- [email protected]~# read
- Hello,World!
- [email protected]~# echo $REPLY
- Hello,World!
- 02). read name # 从标准输入读取输入并赋值给变量name
- [email protected]~# read name
- Jerry
- [email protected]~# echo $name
- Jerry
- 03). read var1 var2 # 第一个变量放置于var1,第二个变量放到var2
- [email protected]~# read firstname lastname
- Jerry Gao
- [email protected]~# echo "firstname:$firstname lastname:$lastname"
- firstname:Jerry lastname:Gao
- 04). read -p "text" # 打印提示‘text‘,等待输入,并将输入存储在REPLY中
- [email protected]~# read -p ‘Please Enter your name:-->‘
- Please Enter your name:-->Jerry
- [email protected]~# echo $REPLY
- Jerry
- 05). read -p "text" var # 打印提示‘text‘,等待输入,并将输入存储在VAR中
- [email protected]~# read -p ‘Please Enter your name:-->‘ name
- Please Enter your name:-->Jerry
- [email protected]~# echo $name
- Jerry
- 06). read -p "text" var1 var2 # 打印提示‘text‘,等待输入,将变量分别存储在var1,var2...
- [email protected]~# read -p ‘What your name? ‘ firstname lastname
- What your name? Jerry Gao
- [email protected]~# echo "Firstname:$firstname Lastname:$lastname"
- Firstname: Jerry Lastname:Gao
- 07). read -r line # 允许输入包含反斜杠
- [email protected]~# read line # 不带-r参数;则反斜杠不显示
- This is line 1. \ This is line 2.
- [email protected]~# echo $line
- This is line 1. This is line 2.
- [email protected]~# read -r line # 带-r参数;则反斜杠正常显示显示
- This is line 1. \ This is line 2.
- [email protected]~# echo $line
- This is line 1. \ This is line 2.
- 08). read -t 5 # 指定读取等待时间为5秒
- [email protected]~# read -t 5 -p ‘Your Name:‘ name
- Your Name:Jerry
- [email protected]~# echo $name # 如果5秒还未输入,则不能输入
- Jerry
- 09). read -a arrayname # 把单词清单读入arrayname的数组里
- [email protected]~# read -a citys
- BJ SH CD GZ
- [email protected]~# echo ${citys[*]}
- BJ SH CD GZ
- [email protected]~# echo ${citys[0]}
- BJ
- 10). read -s -p "pwd:" pwd # 使用-s参数可以不显示用户的输入
- [email protected]~# read -p "Enter Your Password:" -s PASSWORD
- Enter Your Password:
- [email protected]~#
- [email protected]~# echo $PASSWORD # 刚才输入的密码为:1234
- 1234
- 11). read -n 1 -p "Sure?(y/n):" # 使用-n,来确定参数个数
- [email protected]~# read -n 1 -p "Are you sure?(y/n): " ANSWER
- Are you sure?(y/n): y
- [email protected]~#
- [email protected]~# echo -e "Your ANSWER is: $ANSWER"
- Your ANSWER is: y
- 12). read -d ":" var # 使用:作为输入分界符
- [email protected]~# read -d ";" -p "Enter Your Name:" name
- Enter Your Name:Jerry Gao;
- [email protected]~# echo -e "Your Name: $name"
- Your Name: Jerry Gao
read在脚本中的应用:
遍历方式一:
- #!/bin/bash
- count=0
- while read line
- do
- echo -e "$count:-->$line"
- count=$[ count + 1]
- done < /etc/passwd
遍历方式二:
- #!/bin/bash
- awk -F: ‘{print $1,$7}‘ /etc/passwd | while read user bash
- do
- echo -e "USER=$user; BASH=$bash"
- done
以上是关于(转)Shell中read的用法详解的主要内容,如果未能解决你的问题,请参考以下文章