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