使用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! 

 

相关文章:

  • 2022-02-08
  • 2021-12-22
  • 2022-01-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-11
猜你喜欢
  • 2021-10-02
  • 2021-05-24
相关资源
相似解决方案