文章出處
文章列表
使用read命令存在潛在危險,腳本很可能會停下來一直等待腳本用戶輸入數據,如果無論是否輸入數據腳本的必須繼續執行,那么可以使用-t選項指定一個計時器。-t選項指定read命令等待輸入的秒數,當計數器計時數滿時,read命令返回一個非零退出狀態
文件名 test25.sh
#!/bin/bash # timing the date entry if read -t 5 "Please enter your name: " name then echo " Hello $name,welcome to my script" else echo echo "Sorry,too slow!" fi
運行 sh test25.sh
出現 Please enter your name:
輸入admin,出現 Hello admin,welcome to my script
如果等待5s不輸入后出現 Sorry,too slow!
文章列表
全站熱搜