Shell script to accept user input on the fly

I want a shell script that accepts user input simultaneously when performing other tasks. Example: A shell script should echo some messages on the console and when the user presses some keys it should respond to that action.
say, when user presses the key
A - more information should be printed on the console
M - minimum information should be printed on the console
P - pause the script

How to achieve it ?

#!/usr/bin/bash

while true
do
  echo "im in infinite loop.."
done

#simultaneously below logic should be handled

  if [ key "A" is pressed ]
  then
      echo "hello you pressed A while infinite loop is running"
   fi

  if [ key "P" is pressed ]
  then
      echo "pausing the script"
   fi
#!/usr/bin/bash

touch /tmp/$$

( while [ -f /tmp/$$ ]
do
        sleep 1
done ) &

while read -n 1 INPUT
do
case "$INPUT" in
A)     echo "You typed A"
        ;;
Q)    echo "quitting"
        rm /tmp/$$
        break
        ;;
esac
done

wait
1 Like

You only have to search the forums and VOILA!

Bash Inkey Function

1 Like