📄 timed-input.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 + -