• 要找福利?记不住网址?搜不到?如何与我们保持联系?试试 CTRL+D 把零点博客添加进您的浏览器收藏夹,一键直达永不失联!

Linux read 命令

linux系统 涛涛 170次浏览 0个评论

Linux read命令用于从标准输入读取数值。

read 内部命令被用来从标准输入读取单行数据。这个命令可以用来读取键盘输入,当使用重定向的时候,可以读取文件中的一行数据。

语法

<span class="pln">read </span><span class="pun">[-</span><span class="pln">ers</span><span class="pun">]</span> <span class="pun">[-</span><span class="pln">a aname</span><span class="pun">]</span> <span class="pun">[-</span><span class="pln">d delim</span><span class="pun">]</span> <span class="pun">[-</span><span class="pln">i text</span><span class="pun">]</span> <span class="pun">[-</span><span class="pln">n nchars</span><span class="pun">]</span> <span class="pun">[-</span><span class="pln">N nchars</span><span class="pun">]</span> <span class="pun">[-</span><span class="pln">p prompt</span><span class="pun">]</span> <span class="pun">[-</span><span class="pln">t timeout</span><span class="pun">]</span> <span class="pun">[-</span><span class="pln">u fd</span><span class="pun">]</span> <span class="pun">[</span><span class="pln">name </span><span class="pun">...]</span>

参数说明:

  • -a 后跟一个变量,该变量会被认为是个数组,然后给其赋值,默认是以空格为分割符。
  • -d 后面跟一个标志符,其实只有其后的第一个字符有用,作为结束的标志。
  • -p 后面跟提示信息,即在输入前打印提示信息。
  • -e 在输入的时候可以使用命令补全功能。
  • -n 后跟一个数字,定义输入文本的长度,很实用。
  • -r 屏蔽\,如果没有该选项,则\作为一个转义字符,有的话 \就是个正常的字符了。
  • -s 安静模式,在输入字符时不再屏幕上显示,例如login时输入密码。
  • -t 后面跟秒数,定义输入字符的等待时间。
  • -u 后面跟fd,从文件描述符中读入,该文件描述符可以是exec新开启的。

实例

1、简单读取

<span class="com">#!/bin/bash</span>

<span class="com">#这里默认会换行  </span><span class="pln">
echo </span><span class="str">"输入网站名: "</span>  
<span class="com">#读取从键盘的输入  </span><span class="pln">
read website  
echo </span><span class="str">"你输入的网站名是 $website"</span>  
<span class="kwd">exit</span> <span class="lit">0</span>  <span class="com">#退出</span>

测试结果为:

<span class="pun">输入网站名:</span><span class="pln"> 
www</span><span class="pun">.</span><span class="pln">runoob</span><span class="pun">.</span><span class="pln">com
</span><span class="pun">你输入的网站名是</span><span class="pln"> www</span><span class="pun">.</span><span class="pln">runoob</span><span class="pun">.</span><span class="pln">com</span>

2、-p 参数,允许在 read 命令行中直接指定一个提示。

<span class="com">#!/bin/bash</span><span class="pln">

read </span><span class="pun">-</span><span class="pln">p </span><span class="str">"输入网站名:"</span><span class="pln"> website
echo </span><span class="str">"你输入的网站名是 $website"</span> 
<span class="kwd">exit</span> <span class="lit">0</span>

测试结果为:

<span class="pun">输入网站名:</span><span class="pln">www</span><span class="pun">.</span><span class="pln">runoob</span><span class="pun">.</span><span class="pln">com
</span><span class="pun">你输入的网站名是</span><span class="pln"> www</span><span class="pun">.</span><span class="pln">runoob</span><span class="pun">.</span><span class="pln">com</span>

3、-t 参数指定 read 命令等待输入的秒数,当计时满时,read命令返回一个非零退出状态。

<span class="com">#!/bin/bash</span>

<span class="kwd">if</span><span class="pln"> read </span><span class="pun">-</span><span class="pln">t </span><span class="lit">5</span> <span class="pun">-</span><span class="pln">p </span><span class="str">"输入网站名:"</span><span class="pln"> website
</span><span class="kwd">then</span><span class="pln">
    echo </span><span class="str">"你输入的网站名是 $website"</span>
<span class="kwd">else</span><span class="pln">
    echo </span><span class="str">"\n抱歉,你输入超时了。"</span>
<span class="kwd">fi</span>
<span class="kwd">exit</span> <span class="lit">0</span>

执行程序不输入,等待 5 秒后:

<span class="pun">输入网站名:</span>
<span class="pun">抱歉,你输入超时了</span>

4、除了输入时间计时,还可以使用 -n 参数设置 read 命令计数输入的字符。当输入的字符数目达到预定数目时,自动退出,并将输入的数据赋值给变量。

<span class="com">#!/bin/bash</span><span class="pln">

read </span><span class="pun">-</span><span class="pln">n1 </span><span class="pun">-</span><span class="pln">p </span><span class="str">"Do you want to continue [Y/N]?"</span><span class="pln"> answer
</span><span class="kwd">case</span><span class="pln"> $answer </span><span class="kwd">in</span><span class="pln">
Y </span><span class="pun">|</span><span class="pln"> y</span><span class="pun">)</span><span class="pln">
      echo </span><span class="str">"fine ,continue"</span><span class="pun">;;</span><span class="pln">
N </span><span class="pun">|</span><span class="pln"> n</span><span class="pun">)</span><span class="pln">
      echo </span><span class="str">"ok,good bye"</span><span class="pun">;;</span>
<span class="pun">*)</span><span class="pln">
     echo </span><span class="str">"error choice"</span><span class="pun">;;</span>

<span class="kwd">esac</span>
<span class="kwd">exit</span> <span class="lit">0</span>

该例子使用了-n 选项,后接数值 1,指示 read 命令只要接受到一个字符就退出。只要按下一个字符进行回答,read 命令立即接受输入并将其传给变量,无需按回车键。

只接收 2 个输入就退出:

<span class="com">#!/bin/bash</span><span class="pln">

read </span><span class="pun">-</span><span class="pln">n2 </span><span class="pun">-</span><span class="pln">p </span><span class="str">"请随便输入两个字符: "</span><span class="pln"> any
echo </span><span class="str">"\n您输入的两个字符是:$any"</span>
<span class="kwd">exit</span> <span class="lit">0</span>

执行程序输入两个字符:

<span class="pun">请随便输入两个字符:</span> <span class="lit">12</span>
<span class="pun">您输入的两个字符是:</span><span class="lit">12</span>

5、-s 选项能够使 read 命令中输入的数据不显示在命令终端上(实际上,数据是显示的,只是 read 命令将文本颜色设置成与背景相同的颜色)。输入密码常用这个选项。

<span class="com">#!/bin/bash</span><span class="pln">

read  </span><span class="pun">-</span><span class="pln">s  </span><span class="pun">-</span><span class="pln">p </span><span class="str">"请输入您的密码:"</span> <span class="kwd">pass</span><span class="pln">
echo </span><span class="str">"\n您输入的密码是 $pass"</span>
<span class="kwd">exit</span> <span class="lit">0</span>

执行程序输入密码后是不显示的:

<span class="pun">请输入您的密码:</span>
<span class="pun">您输入的密码是</span><span class="pln"> runoob</span>

6.读取文件

每次调用 read 命令都会读取文件中的 “一行” 文本。当文件没有可读的行时,read 命令将以非零状态退出。

通过什么样的方法将文件中的数据传给 read 呢?使用 cat 命令并通过管道将结果直接传送给包含 read 命令的 while 命令。

测试文件 test.txt 内容如下:

<span class="lit">123</span>
<span class="lit">456</span><span class="pln">
runoob</span>

测试代码:

<span class="com">#!/bin/bash</span><span class="pln">
  
count</span><span class="pun">=</span><span class="lit">1</span>    <span class="com"># 赋值语句,不加空格</span><span class="pln">
cat test</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span> <span class="kwd">while</span><span class="pln"> read line      </span><span class="com"># cat 命令的输出作为read命令的输入,read读到>的值放在line中</span>
<span class="kwd">do</span><span class="pln">
   echo </span><span class="str">"Line $count:$line"</span><span class="pln">
   count</span><span class="pun">=</span><span class="pln">$</span><span class="pun">[</span><span class="pln"> $count </span><span class="pun">+</span> <span class="lit">1</span> <span class="pun">]</span>          <span class="com"># 注意中括号中的空格。</span>
<span class="kwd">done</span><span class="pln">
echo </span><span class="str">"finish"</span>
<span class="kwd">exit</span> <span class="lit">0</span>

执行结果为:

<span class="typ">Line</span> <span class="lit">1</span><span class="pun">:</span><span class="lit">123</span>
<span class="typ">Line</span> <span class="lit">2</span><span class="pun">:</span><span class="lit">456</span>
<span class="typ">Line</span> <span class="lit">3</span><span class="pun">:</span><span class="pln">runoob
finish</span>

使用 -e 参数,以下实例输入字符 a 后按下 Tab 键就会输出相关的文件名(该目录存在的):

<span class="pln">$ read </span><span class="pun">-</span><span class="pln">e </span><span class="pun">-</span><span class="pln">p </span><span class="str">"输入文件名:"</span><span class="pln"> str 
</span><span class="pun">输入文件名:</span><span class="pln">a
a</span><span class="pun">.</span><span class="kwd">out</span><span class="pln">    a</span><span class="pun">.</span><span class="pln">py     a</span><span class="pun">.</span><span class="pln">pyc    abc</span><span class="pun">.</span><span class="pln">txt  
</span><span class="pun">输入文件名:</span><span class="pln">a</span>

欢迎转载本文:Linux read 命令
喜欢 (0)
[ld]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址