我想等待 3 秒钟让用户回答“否”。如果是这样,则进程退出,如果用户对提示回答“是”或 3 秒钟过去,则问题会自动给出“y”,进程继续。我尝试使用 read -t 3,但它抱怨说没有这样的选项。
read -s -t 0 -n 9999
read -p "Do you want to continue [DELETE on tbl_summary before \"${TM}\"] [y/s/n]? " confirm
# ******** Saman ********
confirm="y"
#************************
if [[ ${confirm} == "s" ]]; then
kill -STOP $$
elif [[ ${confirm} == "n" ]]; then
exit 1
elif [[ ${confirm} != "y" ]]; then
echo "unknown option. exiting ..."
exit 1
fi
答案1
read -t 3 -p "prompt: " varname # -t to timeout in 3 seconds
: ${varname:=default value} # set the default value if varname is null
echo "$varname"
https://www.gnu.org/software/bash/manual/bashref.html#index-read