这个线程关于 bash 中的非阻塞 I/O可能会有所帮助。
它建议使用stty和dd。
或者您可以使用bash read带有-t 0选项的内置函数。
# do your stuff
# discard rest of input before exiting
while read -t 0 notused; do
read input
echo "ignoring $input"
done
Run Code Online (Sandbox Code Playgroud)
如果您只想在用户位于终端时执行此操作,请尝试以下操作:
# if we are at a terminal, discard rest of input before exiting
if test -t 0; then
while read -t 0 notused; do
read input
echo "ignoring $input"
done
fi
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9012 次 |
| 最近记录: |