使用任意按钮退出正在运行的脚本

使用任意按钮退出正在运行的脚本

我有一个脚本,我试图在其中添加按键时退出的功能。到目前为止,我的代码如下所示:

keyinput=''
if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi
  while [ "x$keyinput" = "x" ]; do
   echo "Press Any Key to Exit."
   echo "Users currently logged on:"
   w #Display who is currently logged on
   echo "Disk space utilization:"
   df -h #Display disk space utilization in human readable form
   echo "Memory and CPU Utilization:"
   ps axo user,pmem,pcpu #Display Username, % of Memory Used, CPU Usage %
  keyinput="`cat -v`"
  done
if [ -t 0 ]; then stty sane; fi
echo "Thanks for using the Live Monitor, you pressed '$keyinput' to exit."

由于某些奇怪的原因,我无法通过任何键输入退出来工作。

答案1

代替

keyinput="`cat -v`"

为了

read -r -n 1 keyinput

相关内容