Press Any Key script sequence using bash - HELP

hi to all.

im a newbie in unix shell scripts. i want to make a simple unix shell script using the bash shell that asks a user to press any key after a series of commands, or an x if he wishes to exit. here's a sample script that i made:

#!/usr/bin/bash
pause(){
   /usr/bin/echo "\t\t Press any key to continue... or [x] to Exit... \c"
   read -s -n 1 any_key
      if [ "$any_key" = "x|X" ]
      then
      exit
      else
      continue
      fi
   /usr/bin/echo
   }
while :
do
   clear
   /usr/bin/echo "\t\t     Sample Press Any Key Routine  \n"
   /usr/bin/echo "\n\t\t Please enter your name :   \c"
   read name
   /usr/bin/echo "\n\n\t\t Your name is $name. \n"
   /usr/bin/echo "\n"
   pause
done

my problem is it does not exit when i press the letter x
it still continues the routine.
any help would be greatly appreciated.

thanks.

Neither "X|x" nor just X|x are valid syntax for using with test ( [...] ) command. For this there is the "case" statement. Use

if [ "$any_key" = x ] || [ "$any_key" = X ]
1 Like

your issue is
"$any_key" = "x|X"

it is taking your key say example you pressed x and comparing with "x|X" :)...you need to use use || to compare two values with if.

if [ ${any_key} = "x" ] || [ ${any_key} = "X" ]
1 Like

@yazu;
@dude2cool;

thanks for the quick replies... my if condition is now working... :slight_smile: