如何用 bash 與使用者互動?

#!/bin/bash
echo -n “Enter your name > ”
read txtname
echo “Hi! $txtname”

上面的 shell script 會接收使用者的輸入. 然後在印出 Hi! 使用者.

如果想要寫個 script 等待 3 秒鐘. 使用者沒回應就取消可以改寫下列程式 :

#!/bin/bash
echo -n “Please input within 3 seconds > ”
if read -t 3 input; then
echo “You just input : $input!”
else
echo “You did not input anything”
fi

大致上是這樣.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.