【转】Linux read命令用法详解

read 命令用法详解

1. 简介

  read是一个buildin命令,主要完成对参数的赋值,类似C语言中的scanf。其不仅可以赋值变量,还可以赋值数组;其输入不仅是屏幕,还可以是文件描述符。

2. man中选项说明

              -a aname

                     The words are assigned to sequential indices  of  the  array  variable

                     aname,  starting  at  0.   aname  is  unset  before any new values are

                     assigned.  Other name arguments are ignored.

              -d delim

                     The first character of delim is used  to  terminate  the  input  line,

                     rather than newline.

              -e     If  the  standard input is coming from a terminal, readline (see READ-

                     LINE above) is used to obtain the line.

              -n nchars

                     read returns after reading nchars characters rather than waiting for a

                     complete line of input.

              -p prompt

                     Display  prompt  on standard error, without a trailing newline, before

                     attempting to read any input.  The prompt is displayed only  if  input

                     is coming from a terminal.

              -r     Backslash  does not act as an escape character.  The backslash is con-

                     sidered to be part of the line.  In  particular,  a  backslash-newline

                     pair may not be used as a line continuation.

              -s     Silent  mode.   If input is coming from a terminal, characters are not

                     echoed.

              -t timeout

                     Cause read to time out and return failure if a complete line of  input

                     is not read within timeout seconds.  This option has no effect if read

                     is not reading input from the terminal or a pipe.

              -u fd  Read input from file descriptor fd.

-a 后跟一个变量,该变量会被认为是个数组,然后给其赋值,默认是以空格为分割符。

-d 后面跟一个标志符,其实只有其后的第一个字符有用,作为结束的标志,会举例说  明。

-p 后面跟提示信息,即在输入前打印提示信息。

-e 在输入的时候可以时候命令补全功能。

-n 后跟一个数字,定义输入文本的长度,很实用。

-r 屏蔽\,如果没有该选项,则\作为一个转义字符,有的话 \就是个正常的字符了。

-s 安静模式,在输入字符时不再屏幕上显示,例如login时输入密码。

-t 后面跟秒数,定义输入字符的等待时间。

-u 后面跟fd,从文件描述符中读入,该文件描述符可以是exec新开启的。

3. 举例

 -a举例:


root@localhost:~/test#read -a tao

qq ww ee rr

root@localhost:~/test#echo ${tao[1]}

Ww

-b举例:

root@localhost:~/test#read -d eof -a tao

ww

dd

gg

hh  (输入一个e)

root@localhost:~/test#echo ${tao[3]}

Hh

-n举例:

root@localhost:~/test#read -n3 -p "you can input 3 word:"

you can input 3 word:xxx

(输入3个字符后自动退出!)

-s举例:

root@localhost:~/test#read -p password: -s passwd

password:

(这时候输入就不再显示在屏幕上了!)

-e举例:

root@localhost:~/test#read -e file

(tab键补全)

exp1               file               ngis_post.sh       text              

exp5               linux-2.6.27.54/   test               xen-3.0.1-install/


-u举例:

root@localhost:~/test#exec 3<file

root@localhost:~/test#read -u 3 tao

root@localhost:~/test#echo $tao

hello world!

root@localhost:~/test#

注意看下读入的次序:

root@localhost:~/test#cat file

hello world!

i am good

root@localhost:~/test#exec 3<file

root@localhost:~/test#read -u 3 tao

root@localhost:~/test#echo $tao

hello world!

root@localhost:~/test#read -u 3 tao

root@localhost:~/test#echo $tao

i am good

root@localhost:~/test#

这个选项很有用的,特别是自循环读入的时候。 

-----------------------------------------------------------------------------------------------------

 

linux read 命令

1、基本读取

read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说)。得到输入后,read命令将数据放入一个标准变量中。下面是read命令

的最简单形式::

#!/bin/bash

echo -n "Enter your name:"   //参数-n的作用是不换行,echo默认是换行

read  name                   //从键盘输入

echo "hello $name,welcome to my program"     //显示信息

exit 0                       //退出shell程序。

//********************************

由于read命令提供了-p参数,允许在read命令行中直接指定一个提示。

所以上面的脚本可以简写成下面的脚本::

#!/bin/bash

read -p "Enter your name:" name

echo "hello $name, welcome to my program"

exit 0

在上面read后面的变量只有name一个,也可以有多个,这时如果输入多个数据,则第一个数据给第一个变量,第二个数据给第二个变量,如果输入数据个数过多,则最后所有的值都给第一个变量。如果太少输入不会结束。

//*****************************************

read命令行中也可以不指定变量.如果不指定变量,那么read命令会将接收到的数据放置在环境变量REPLY中。

例如::

read -p "Enter a number"

环境变量REPLY中包含输入的所有数据,可以像使用其他变量一样在shell脚本中使用环境变量REPLY.

2、计时输入.

使用read命令存在着潜在危险。脚本很可能会停下来一直等待用户的输入。如果无论是否输入数据脚本都必须继续执行,那么可以使用-t选项指定一个计时器。

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

#!/bin/bash

if read -t 5 -p "please enter your name:" name

then 

    echo "hello $name ,welcome to my script"

else

    echo "sorry,too slow"

fi

exit 0

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

#!/bin/bash

read –n 1 -p "Do you want to continue [Y/N]?" answer

case $answer in

Y | y)

      echo "fine ,continue";;

N | n)

      echo "ok,good bye";;

*)

     echo "error choice";;

esac

exit 0

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

 

3、默读(输入不显示在监视器上)

有时会需要脚本用户输入,但不希望输入的数据显示在监视器上。典型的例子就是输入密码,当然还有很多其他需要隐藏的数据。

-s选项能够使read命令中输入的数据不显示在监视器上(实际上,数据是显示的,只是 read命令将文本颜色设置成与背景相同的颜色)。

#!/bin/bash

read  -s  -p "Enter your password:" pass

echo "your password is $pass"

exit 0 

4、读文件

最后,还可以使用read命令读取Linux系统上的文件。

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

读取文件的关键是如何将文本中的数据传送给read命令。

最常用的方法是对文件使用cat命令并通过管道将结果直接传送给包含read命令的 while命令

例子::

#!/bin/bash

count=1    //赋值语句,不加空格

cat test | while read line        //cat 命令的输出作为read命令的输入,read读到的值放在line

do

   echo "Line $count:$line"

   count=$[ $count + 1 ]          //注意中括号中的空格。

done

echo "finish"

exit 0

 

原文地址:https://www.cnblogs.com/fzzl/p/2796099.html