1

I have a script in which I'm trying to add the functionality of exiting upon keypress. Thus far, my code looks like what you see below:

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."

For some odd reason, I can not get the exit on any key input to work.

2

Replace

keyinput="`cat -v`"

For

read -r -n 1 keyinput
2
  • Thanks, that worked. One other question, let's assume I call that script that from another script with a line like wait -n 5 ./thisscript.sh. When I try to quit with the keypress, it doesn't work. However, when the 'thisscript.sh' is run by itself, the key press on exit works just fine – Linuxn00b Dec 6 '15 at 22:30
  • Please open a new question, that is a very different issue. It is the usual action to accept an answer when it solves the question. Will you accept this answer, Please?. – user79743 Dec 6 '15 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.