什么是DOS的暂停命令的bash等价物?
我需要在shell脚本上暂停以在继续之前显示警告。 例如,在DOS上它是这样的:
doit.bat:
[...] echo 'Are you sure? Press Ctrl-C to abort, enter to continue.' pause [...]
我怎么能在bash上这样做? 目前,睡眠命令似乎可以解决问题并且很简单,但并不完全是这样的想法:
doit.sh
[...] echo 'Are you sure? Press Ctrl-C to abort.' sleep 3 [...]
一些东西
echo -n "prompt" #'-n' means do not add \n to end of string read # No arg means dump next line of input
read -p "Press any key to continue or CTRL-C to abort"
在我的脚本中14.04下工作正常。 正如@Lekensteyn在上面的评论中指出的那样,自12.04.4以来就是这种情况。 “帮助阅读”页面指出:
-p prompt output the string PROMPT without a trailing newline before attempting to read
我最喜欢使用它是设置默认超时,以便脚本也可以无人值守运行:
echo "Press any key to continue..." read -n1 -t5 any_key
哪里:
-
-n1
告诉read接受任何单个字符 -
-t5
告诉它等待最多5秒的输入
我几乎完全使用CentOS下的Bash工作,所以我无法保证在其他Linux版本上工作,但由于它是Bash,我希望它可以工作。 我很想知道它是否确实存在! 🙂
等待停止脚本的简单方法是从脚本中调用kill -STOP $$
:暂停后脚本将在收到-CONT信号后继续工作。
#!/bin/bash echo "$0 stopping now" kill -STOP $$ echo "$0 continues its work"
echo "press enter to continue" read unusedVariable echo "running these 3 lines in .sh file, this echo will never be seen, unless you have a really slow computer"
将这3行放在.sh文件中并运行它