read命令作用:
從標準輸入中讀取一行,並把輸入行的每個字段的值指定給shell變量
參數:-a 後跟一個變量,該變量會被認爲是個數組,然後給其賦值,默認是以空格爲分割符。
-d 後面跟一個標誌符,其實只有其後的第一個字符有用,作爲結束的標誌,會舉例說 明。
-p 後面跟提示信息,即在輸入前打印提示信息。
-e 在輸入的時候可以時候命令補全功能。
-n 後跟一個數字,定義輸入文本的長度,很實用。
-r 屏蔽,如果沒有該選項,則作爲一個轉義字符,有的話 就是個正常的字符了。
-s 安靜模式,在輸入字符時不再屏幕上顯示,例如login時輸入密碼。
-t 後面跟秒數,定義輸入字符的等待時間。
-u 後面跟fd,從文件描述符中讀入,該文件描述符可以是exec新開啓的。
示例:
$ vi shell_0831.sh
#!/bin/bash
echo "**************************read command**************************"
#read -p and -t
echo "----1.read -p and -t ----"
read -t 20 -p "please enter your name:" name
echo $name
#read -s -p
echo "----2.read -s and -p----"
read -s -p "please enter your pass:" pass
echo -e "\n"
echo "your pass is $pass"
#read -d
echo "----3.read -d----"
read -d e
echo "end as e"
運行腳本程序:
$ ./shell_0831.sh
**************************read command**************************
----1.read -p and -t ----
please enter your name:jack
jack
----2.read -s and -p----
please enter your pass:
your pass is 123
----3.read -d----
abcd
ffff
eend as e