深圳住房建设局网站申报,wordpress地产主题,百城建设提质工程网站,怎么免费制作公司网页*更新日志 *2023-12-18 1.根据[美] 威廉肖特斯 #xff08;Willian shotts#xff09;所著《Linux命令行大全#xff08;第2版#xff09;》 更新了-e、-i、-r选项的说明 2.更新了 2.8 的实例#xff0c;增加了gif动图 3.补充了-i的应用实例 2.1… *更新日志 *2023-12-18 1.根据[美] 威廉·肖特斯 Willian shotts所著《Linux命令行大全第2版》 更新了-e、-i、-r选项的说明 2.更新了 2.8 的实例增加了gif动图 3.补充了-i的应用实例 2.12 目录
目录0 前言1 read命令的功能、格式、返回值和注意 1.1 命令功能1.2 命令格式1.3 返回值1.4 注意事项2 命令应用实例 2.1 一次读入多个变量值2.2 不指定变量名2.3 测试read命令的返回值2.3 指定输入时限并进行相应处理2.4 -t 指定结束符2.5 -n 指定输入字符个数2.6 -N 指定输入字符个数2.7 -s不回显来自终端的输入2.8 -e使用命令补全功能2.9 -r 允许输入的值中包含的反斜杠\也作为值输出2.10 -a 读取数组值2.11 -u指定文件说明符2.12 -i 使用初始值 0 前言 在交互式编程中有时我们需要用户先通过键盘来输入数据然后程序根据用户输入的数据来做相应的处理。
在之前的学习中我们已经使用read命令来读取用户通过键盘输入的数据但对read命令没有做进一步的说明。
现在我们来研究一下read命令的详细用法。
1 read命令的功能、格式、返回值和注意
我们可以使用命令 help read 来查看seq命令的帮助信息 purleEndurer bash ~ $ help read read: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] Read a line from the standard input and split it into fields. Reads a single line from the standard input, or from file descriptor FD if the -u option is supplied. The line is split into fields as with word splitting, and the first word is assigned to the first NAME, the second word to the second NAME, and so on, with any leftover words assigned to the last NAME. Only the characters found in $IFS are recognized as word delimiters. If no NAMEs are supplied, the line read is stored in the REPLY variable. Options: -a array assign the words read to sequential indices of the array variable ARRAY, starting at zero -d delim continue until the first character of DELIM is read, rather than newline -e use Readline to obtain the line in an interactive shell -i text Use TEXT as the initial text for Readline -n nchars return after reading NCHARS characters rather than waiting for a newline, but honor a delimiter if fewer than NCHARS characters are read before the delimiter -N nchars return only after reading exactly NCHARS characters, unless EOF is encountered or read times out, ignoring any delimiter -p prompt output the string PROMPT without a trailing newline before attempting to read -r do not allow backslashes to escape any characters -s do not echo input coming from a terminal -t timeout time out and return failure if a complete line of input is not read withint TIMEOUT seconds. The value of the TMOUT variable is the default timeout. TIMEOUT may be a fractional number. If TIMEOUT is 0, read returns success only if input is available on the specified file descriptor. The exit status is greater than 128 if the timeout is exceeded -u fd read from file descriptor FD instead of the standard input Exit Status: The return code is zero, unless end-of-file is encountered, read times out, or an invalid file descriptor is supplied as the argument to -u. readarray: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array] Read lines from a file into an array variable. A synonym for mapfile. readonly: readonly [-aAf] [name[value] ...] or readonly -p Mark shell variables as unchangeable. Mark each NAME as read-only; the values of these NAMEs may not be changed by subsequent assignment. If VALUE is supplied, assign VALUE before marking as read-only. Options: -a refer to indexed array variables -A refer to associative array variables -f refer to shell functions -p display a list of all readonly variables and functions An argument of -- disables further option processing. Exit Status: Returns success unless an invalid option is given or NAME is invalid. purleEndurer bash ~ $ 1.1 命令功能
从标准输入或-u 选项指定的文件描述符读取一行。该行被拆分为多个字段就像单词拆分一样第一个单词分配给第一个 变量第二个单词分配给第二个 变量依此类推任何剩余的单词都分配给最后一个 变量。如果未提供 变量名则读取的行存储在 REPLY 变量中。
只有在 $IFS 中找到的字符才会被识别为单词分隔符。
1.2 命令格式 read [-ers] [-a 索引数组名] [-d 结束符] [-i 初始字符串] [-n 字符数] [-N 字符数] [-p 提示字符串] [-t 超时秒数] [-u 文件描述符] [变量名 ...] 选项说明
选项说明备注-a 索引数组名将读取的单词从下标零开始分配给数组名指定的 索引数组默认是以空格为分割符array-d 结束符读取数据直至结束符而不是换行符delimiter-e使用 在交互式 shell 中获取用户输入行在输入的时候可以使用命令补全功能-i 初始字符串 使用初始字符串作为 用户输入行 的初始值 需要与-e选项一起使用 initialize-n 字符数在读取指定的字符数后返回除非遇到定界符、换行符或读取超时定界符包括\ttab键。number-N 字符数 在读取指定的字符数后返回除非遇到 EOF 或读取超时 与 -n 不同的是-N 会忽略任何定界符。 number-p 提示字符串先输出提示字符串再读取数据prompt-r不允许反斜杠转义任何字符raw mode-s不回显来自终端的输入silence-t 超时秒数 指定输入字符的等待时间。 如果在 超时秒数 内未读取完整的输入行则超时并返回失败。 TMOUT变量的值是默认超时秒数。 超时秒数 可以是小数。 如果 超时秒数 为 0则仅当指定的文件描述符上有可用的输入时读取才会返回成功。 如果超过超时则退出状态大于 128 timeout-u 文件描述符从指定的文件描述符读入数据use
1.3 返回值
命令正常执行时返回值为零
如果遇到文件末尾、读取超时或提供无效文件描述符作为 -u 的参数时返回值不为零。
如果读取超时返回值 将 大于 128。
1.4 注意事项
变量名列表 应该放在 命令的最后。变量名之间以空格分隔。-i选项需要与-e选项一起使用才有用 2 命令应用实例
2.1 一次读入多个变量值
例提示用户输入3个整数作为a、c、c 三个变量的值 purpleEndurer bash ~ $ read -p Enter there integers: a b c Enter there integers:1 2 3 purpleEndurer bash ~ $ echo a$a b$b c$c a1 b2 c3 purpleEndurer bash ~ $ read -p Enter there integers: a b c Enter there integers:1 2 3 4 5 6 purpleEndurer bash ~ $ echo a$a b$b c$c a1 b2 c3 4 5 6 purpleEndurer bash ~ $ 当我们输入1 2 3 这三个整数后变量a的值为1变量b的值为2变量c的值为3。
当我们输入1 2 3 4 5 6 这六个整数后由于我们只给了a、b、c三个变理名所以变量a的值为1变量b的值为2而变量c的值为3 4 5 6。 2.2 不指定变量名
例提示用户输入3个整数但没指定变量名 purpleEndurer bash ~ $ echo $REPLY purpleEndurer bash ~ $ read -p Enter there integers: Enter there integers:1 2 3 purpleEndurer bash ~ $ echo $REPLY 1 2 3 purpleEndurer bash ~ $ 由于我们没有指定变量名所以输入的值1 2 3保存在变量REPLY中。 2.3 测试read命令的返回值
例提示用户输入名字输入时间限定为10秒钟 purpleEndurer bash ~ $ read -t 10 -p Enter your name: Enter your name:pe purpleEndurer bash ~ $ echo $? 0 purpleEndurer bash ~ $ read -t 10 -p Enter your name: Enter your name:purpleEndurer bash ~ $ echo $? 142 purpleEndurer bash ~ $ read -t 10 -p Enter your name: Enter your name:purpleEndurer bash ~ $ echo $? 1 purpleEndurer bash ~ $ read -t 10 -p Enter your name: Enter your name:^C purpleEndurer bash ~ $ echo $? 130 purpleEndurer bash ~ $ 当我们在10秒钟内输入用户名pe并回车read命令正常执行返回值为0
当我们未在10秒钟内输入用户名read命令返回值为142大于128
当我们输入^D时read命令返回值为1
当我们输入^C时read命令返回值为130 2.3 指定输入时限并进行相应处理 例编写一个脚本提示用户在5秒钟内输入名字如果用户输入名字则对用户进行问候否则提示用户没有输入名字。
脚本内容如下
read -t 5 -p Enter you name in 5 seconds: name # 提示用户在5秒钟内输入名字并保存在变量name中
if [ $? 0 ]; then # 如果用户在5秒钟内输入了名字那么read命令返回值为0echo Hello, $name. # 对用户进行问候
elseecho -e \nYou do not enter your name. # 提示用户没有输入名字
fi purpleEndurer bash ~ $ cp /dev/stdin a.shread -t 5 -p Enter you name in 5 seconds: name if [ $? 0 ]; then echo Hello, $name. else echo -e \nYou do not enter your name. fi purpleEndurer bash ~ $ cat a.sh read -t 5 -p Enter you name in 5 seconds: name if [ $? 0 ]; then echo Hello, $name. else echo -e \nYou do not enter your name. fi purpleEndurer bash ~ $ . a.sh Enter you name in 5 seconds:pe Hello, pe. purpleEndurer bash ~ $ . a.sh Enter you name in 5 seconds: You do not enter your name. purpleEndurer bash ~ $ 2.4 -t 指定结束符
例 读取用户输入值存入变量a指定#作为结束符 purpleEndurer bash ~ $ read -d # a ab#purpleEndurer bash ~ $ echo $a ab purpleEndurer bash ~ $ 当我们输入ab#时读取结束。变量a的值为ab。
2.5 -n 指定输入字符个数
例 读取5个字符分别保存到变量a和b purpleEndurer bash ~ $ read -n 5 a b12345purpleEndurer bash ~ $ echo a$a b$b a12345 b purpleEndurer bash ~ $ read -n 5 a b12 34purpleEndurer bash ~ $ echo a$a b$b a12 b34 purpleEndurer bash ~ $ read -n 5 a b1 234purpleEndurer bash ~ $ echo a$a b$b a1 b234 purpleEndurer bash ~ $ read -n 5 a b12 purpleEndurer bash ~ $ echo a$a b$b a12 b purpleEndurer bash ~ $ read -n 5 a b123^C purpleEndurer bash ~ $ echo a$a b$b a12 b purpleEndurer bash ~ $ 当我们输入12345时变量a的值为12345变量b值为空
当我们输入12空格34时变量a的值为12变量b值为34
当我们输入1[Tab]234时变量a的值为1变量b值为234
当我们输入12回车时变量a的值为12变量b值为空
当我们输入123^C时read命令被终止了所以变量a的值没有变化仍为12变量b值没有变化仍为空 2.6 -N 指定输入字符个数
例 读取5个字符分别保存到变量a和b purpleEndurer bash ~ $ read -N 5 a b12 34purpleEndurer bash ~ $ echo a$a b$b a12 34 b purpleEndurer bash ~ $ read -N 5 a b12 34purpleEndurer bash ~ $ echo a$a b$b a12 34 b purpleEndurer bash ~ $ read -d # -N 5 a b12#34purpleEndurer bash ~ $ echo a$a b$b a12#34 b purpleEndurer bash ~ $ 当我们输入12[Tab]34时变量a的值为12 34变量b值为空
当我们输入12空格34时变量a的值为12 34变量b值为空
即使我们用-t # 选项指定#作为结束符输入12#34结果变量a的值为12#34变量b值为空
看来使用-N选项后空格或[Tab]不再作为字符串值的分隔符-t选项也不起作用这样只能将读取值存到第1个变量中了。 2.7 -s不回显来自终端的输入
例提示用户输入密码输入时不回显输入完成后再显示用户输入的密码。 purpleEndurer bash ~ $ read -s -p Enter your password: p Enter your password:purpleEndurer bash ~ $ echo Your password is $p Your password is abcd purpleEndurer bash ~ $ 当用户输入密码abcd时并不回显按回车键后才显示用户输入的密码是abcd。
2.8 -e使用命令补全功能 PurpleEndurer bash ~ $ ls Code PurpleEndurer bash ~ $ read -e aCode/ PurpleEndurer bash ~ $ echo $a Code/ PurpleEndurer bash ~ $ read -e acde PurpleEndurer bash ~ $ echo $a cde PurpleEndurer bash ~ $ 我们先用ls命令查看当前目录内容存在一个名Code的文件或文件夹
然后我们用命令 read -e a 来读取输入存到变量a
在我们输入大写字符C后按Tab键启用命令补全功能输入行的内容就变为Code/按下回车键。
再用echo $a 查看变量a的值为Code/
在我们输入大写字符C后按Tab键启用命令补全功能输入行的内容就变为Code/我们用删除键Backspace删除Code/输入cde回车再用echo $a 查看变量a的值为cde 2.9 -r 允许输入的值中包含的反斜杠\也作为值输出 purpleEndurer bash ~/Code $ read a//\c purpleEndurer bash ~/Code $ echo $a //c purpleEndurer bash ~/Code $ read -r a//\c purpleEndurer bash ~/Code $ echo $a //\c purpleEndurer bash ~/Code $ 对于命令 read a输入 //\c存储到变量a的值为 //C不包含反斜杠\。
对于命令 rread -r a输入 //\c存储到变量a的值为 //\C包含反斜杠\。 2.10 -a 读取数组值 数组是最常用的一种数据结构在之前的
Linux shell编程学习笔记15定义数组、获取数组元素值和长度、数组拼接或合并_linux获取数组长度-CSDN博客https://blog.csdn.net/Purpleendurer/article/details/134009982
和
Linux shell编程学习笔记16bash中的关联数组-CSDN博客https://blog.csdn.net/Purpleendurer/article/details/134053506中我们学习了Lininux Shell编程中的数组的定义和赋值的方法其中对数组元素的赋值方法 我们主要介绍了在定义数组时同时进行初始化和定义数组后用赋值语句来赋值两种方法。
现在我们也可以用read命令来将用户输入的值存储到数组
例从键盘读取输入存入数组a purpleEndurer bash ~/Code $ read -a a↙1 2 3↙ purpleEndurer bash ~/Code $ echo ${a[*]}↙ 1 2 3 purpleEndurer bash ~/Code $ echo ${a[0]}↙ 1 purpleEndurer bash ~/Code $ i当我们输入 1 2 3那么 a[0]1a[1]2a[2]3
可惜的是read命令不能将用户输入存储到关联数组 purpleEndurer bash ~/Code $ declare -A A↙ purpleEndurer bash ~/Code $ read -a A↙a b c↙ bash: read: A: cannot convert associative to indexed array purpleEndurer bash ~/Code $ 我们先用命令 声明一个关联数组A
然后用命令 来读取用户输入存储到关联数组A但是出错了bash: read: A: cannot convert associative to indexed array
2.11 -u指定文件说明符
例将文件a.txt和b.txt的内容逐行拼接显示。 purpleEndurer bash ~ $ cp /dev/stdin a.txt↙111↙222↙333↙444↙ purpleEndurer bash ~ $ cp /dev/stdin b.txt↙aaa↙bbb↙ccc↙ddd↙eee↙ purpleEndurer bash ~ $ while read -u3 i read -u4 j;do echo $i $j; done 3a.txt 4b.txt↙ 111 aaa 222 bbb 333 ccc 444 ddd purpleEndurer bash ~ $ 如果我们希望在每一行前显示行号那么我们可以增加一个变量n用来记录当前行数 purpleEndurer bash ~ $ n1;while read -u3 i read -u4 j;do echo Line $n: $i $j;n$[ $n 1 ]; done 3a.txt 4b.txt↙ Line 1: 111 aaa Line 2: 222 bbb Line 3: 333 ccc Line 4: 444 ddd purpleEndurer bash ~ $ 2.12 -i 使用初始值
例 提示用户输入用户名 存储到变量a中用户名初始值为 Anonymous PurpleEndurer bash ~ $ read -p Enter your name: -i Anonymous a Enter your name:↙ PurpleEndurer bash ~ $ echo $a PurpleEndurer bash ~ $ read -p Enter your name: -i Anonymous -e a Enter your name:Anonymous↙ PurpleEndurer bash ~ $ echo $a↙ Anonymous PurpleEndurer bash ~ $ read -p Enter your name: -i Anonymous -e a Enter your name:abc↙ PurpleEndurer bash ~ $ echo $a↙ abc PurpleEndurer bash ~ $ 在执行 命令 read -p Enter your name: -i Anonymous a 时虽然我们使用 -i Anonymous选项但执行时并没有显示初始值 Anonymous我们直接回车变量a的值为空
在执行 命令 read -p Enter your name: -i Anonymous -e a 时我们在上面的命令中增加了 -e选项在执行时就显示出初始值 Anonymous我们直接回车变量a的值为就为 Anonymous
在执行 命令 read -p Enter your name: -i Anonymous -e a 时我们在上面的命令中增加了 -e选项在执行时就显示出初始值 Anonymous我们按删除键Backspace删除 Anonymous输入abc回车变量a的值为abc