Read可以带有-a, -d, -e, -n, -p, -r, -t等等选项。
随便在Linux终端上敲入read命令,如下:
root@bigdata# read
dawd^H^H^H^[[A^[[B^[[D^[[C^[OP^[OQ^[OR^[OS
注:我输入的分别是dawd、backspace键、↑、↓、←、→、F1、F2、F3、F4
看到这些功能键变成了原来的字符组合,并且功能键的功能失效了!
解决办法:
采用 read -e参数
root@bigdata# read -e
dawd
现在backspace键、↑、↓、←、→、F1、F2、F3、F4都可以使用了
=================================================================================
拓展组合参数:
可以结合 -p 使用提示语句
组合后格式为:
read -ep
其他用法:
read的常用用法如下:
read -[pstnd] var1 var2 ...
-p 提示语句
-n 字符个数
-s 屏蔽回显
-t 等待时间
-d 输入分界
用法举例:
01). read # 从标准输入读取一行并赋值给特定变量REPLY
root@linux~# read
Hello,World!
root@linux~# echo $REPLY
Hello,World!
02). read name # 从标准输入读取输入并赋值给变量name
root@linux~# read name
Jerry
root@linux~# echo $name
Jerry
03). read var1 var2 # 第一个变量放置于var1,第二个变量放到var2
root@linux~# read firstname lastname
Jerry Gao
root@linux~# echo "firstname:$firstname lastname:$lastname"
firstname:Jerry lastname:Gao
04). read -p "text" # 打印提示'text',等待输入,并将输入存储在REPLY中
root@linux~# read -p 'Please Enter your name:-->'
Please Enter your name:-->Jerry
root@linux~# echo $REPLY
Jerry
05). read -p "text" var # 打印提示'text',等待输入,并将输入存储在VAR中
root@linux~# read -p 'Please Enter your name:-->' name
Please Enter your name:-->Jerry
root@linux~# echo $name
Jerry
06). read -p "text" var1 var2 # 打印提示'text',等待输入,将变量分别存储在var1,var2...
root@linux~# read -p 'What your name? ' firstname lastname
What your name? Jerry Gao
root@linux~# echo "Firstname:$firstname Lastname:$lastname"
Firstname: Jerry Lastname:Gao
07). read -r line # 允许输入包含反斜杠
root@linux~# read line # 不带-r参数;则反斜杠不显示
This is line 1. \ This is line 2.
root@linux~# echo $line
This is line 1. This is line 2.
root@linux~# read -r line # 带-r参数;则反斜杠正常显示显示
This is line 1. \ This is line 2.
root@linux~# echo $line
This is line 1. \ This is line 2.
08). read -t 5 # 指定读取等待时间为5秒
root@linux~# read -t 5 -p 'Your Name:' name
Your Name:Jerry
root@linux~# echo $name # 如果5秒还未输入,则不能输入
Jerry
09). read -a arrayname # 把单词清单读入arrayname的数组里
root@linux~# read -a citys
BJ SH CD GZ
root@linux~# echo ${citys[*]}
BJ SH CD GZ
root@linux~# echo ${citys[0]}
BJ
10). read -s -p "pwd:" pwd # 使用-s参数可以不显示用户的输入
root@linux~# read -p "Enter Your Password:" -s PASSWORD
Enter Your Password:
root@linux~#
root@linux~# echo $PASSWORD # 刚才输入的密码为:1234
1234
11). read -n 1 -p "Sure?(y/n):" # 使用-n,来确定参数个数
root@linux~# read -n 1 -p "Are you sure?(y/n): " ANSWER
Are you sure?(y/n): y
root@linux~#
root@linux~# echo -e "Your ANSWER is: $ANSWER"
Your ANSWER is: y
12). read -d ":" var # 使用:作为输入分界符
root@linux~# read -d ";" -p "Enter Your Name:" name
Enter Your Name:Jerry Gao;
root@linux~# echo -e "Your Name: $name"
Your Name: Jerry Gao