⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timed-input.sh

📁 Shall高级编程
💻 SH
字号:
#!/bin/bash# timed-input.sh# TMOUT=3    Also works, as of newer versions of Bash.TIMELIMIT=3  # Three seconds in this instance. May be set to different value.PrintAnswer(){  if [ "$answer" = TIMEOUT ]  then    echo $answer  else       # Don't want to mix up the two instances.     echo "Your favorite veggie is $answer"    kill $!  # Kills no longer needed TimerOn function running in background.             # $! is PID of last job running in background.  fi}  TimerOn(){  sleep $TIMELIMIT && kill -s 14 $$ &  # Waits 3 seconds, then sends sigalarm to script.}  Int14Vector(){  answer="TIMEOUT"  PrintAnswer  exit 14}  trap Int14Vector 14   # Timer interrupt (14) subverted for our purposes.echo "What is your favorite vegetable "TimerOnread answerPrintAnswer#  Admittedly, this is a kludgy implementation of timed input,#+ however the "-t" option to "read" simplifies this task.#  See "t-out.sh", below.#  If you need something really elegant...#+ consider writing the application in C or C++,#+ using appropriate library functions, such as 'alarm' and 'setitimer'.exit 0

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -