本講內容主要講解read命令。經過學習咱們已經知道如何查詢Bash內置命令幫助。首先查看read命令幫助文檔。bash
[root@promote ~]# man read [root@promote ~]# [root@promote ~]# info read | less [root@promote ~]#
read 經常使用參數有-p 、-t、和-s。less
下文代碼將分別演示簡單用法。學習
#-p 等待用戶輸入內容 [root@promote ~]# read -p "please input name: " name please input name: www #-t 設置等待時間,默認秒 [root@promote ~]# cat testreadv1.0.sh #!/bin/bash if read -t 5 -p "please input name: " name then echo "name is : $name" else echo "input error..." fi exit 0 [root@promote ~]# cat testreadv1.0.sh #!/bin/bash if read -t 5 -p "please input name: " name then echo "name is : $name" else echo -e "\ninput error..." fi exit 0 [root@promote ~]# bash testreadv1.0.sh please input name: www name is : www #第二次等待5秒未輸入內容 [root@promote ~]# bash testreadv1.0.sh please input name: www input error... #思考問題:如何清除錯誤輸出內容到下一行,本次操做實際輸入www未按回車鍵 [root@promote ~]# www #-s 不前臺顯示輸入內容 [root@promote ~]# cat testreadv1.1.sh #!/bin/bash read -s -p "please input password:" passwd echo -e "\npassword is :$passwd" exit 0 [root@promote ~]# [root@promote ~]# bash testreadv1.1.sh please input password: password is :www [root@promote ~]#