Press Any Key script sequence using bash - HELP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Press Any Key script sequence using bash - HELP
# 1  
Old 08-11-2011
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:
Code:
#!/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.
# 2  
Old 08-11-2011
Neither "X|x" nor just X|x are valid syntax for using with test ( [...] ) command. For this there is the "case" statement. Use
Code:
if [ "$any_key" = x ] || [ "$any_key" = X ]

This User Gave Thanks to yazu For This Post:
# 3  
Old 08-11-2011
your issue is
"$any_key" = "x|X"

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

Code:
if [ ${any_key} = "x" ] || [ ${any_key} = "X" ]

This User Gave Thanks to dude2cool For This Post:
# 4  
Old 08-11-2011
@yazu;
@dude2cool;

thanks for the quick replies... my if condition is now working... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a command for specific amount of time with an auto key press

Hi, I have been trying to do a small fun project for myself. I want to run a command for 45 seconds. And to get the final output of this command, the script requires I push the "q" key on my keyboard and then the final output file becomes available. I tried the following script. But it... (12 Replies)
Discussion started by: jacobs.smith
12 Replies

2. Shell Programming and Scripting

How to detect key press in cgi shell script?

I want to detect key pressed in my .cgi web page, but it does not work even I found the code in other web site. My code is : #!/bin/sh #================================================= # PATH defination # ================================================... (2 Replies)
Discussion started by: Shuinvy
2 Replies

3. UNIX for Dummies Questions & Answers

bash script to parse sequence...

Hi, I have 4000 list files and 4000 sequence data files. Each list file contains a number of 'headers' and data file contains 'header and data'. I would like to extract data from the data file using the list file and write into a new file. As each of the files are quite large, an efficient piece... (6 Replies)
Discussion started by: Fahmida
6 Replies

4. Shell Programming and Scripting

Any key press causing logout from shell

Hi all! I have written a shell script which will invoke perl script infinitly in the background in a loop. Code will do as:Within while loop, perl script will be run in background, get the pid and notify pid in though mail. then wait for pid to be completed before going for next iteration. I am... (1 Reply)
Discussion started by: jramesh1
1 Replies

5. Shell Programming and Scripting

read -n1 -r -p "Press..." key / produces error in bash shell script

Hello! Sorry, for my not so perfect english! I want to stop bash shell script execution until any key is pressed. This line in a bash shell script read -n1 -r -p "Press any key to continue..." key produces this error When I run this from the command line usera@lynx:~$ read... (4 Replies)
Discussion started by: linuxinho
4 Replies

6. Shell Programming and Scripting

Bash script [Press Tab] Screen Blank..

Dear Member, OLD Question --> disable-completion not solved My bash Menu script ping process problem. If ping still running and users press SCREEN is Blank... Cant Members help me.. kill signal or others scripting for my case, btw i use Linux.. Thanks, Rico My Bash Script : ... (1 Reply)
Discussion started by: carnegiex
1 Replies

7. Shell Programming and Scripting

Trap key press in a script

How can I trap a character press in the shell script. For eg:- I have a script runinng a infinite loops , I will need to quit if q is pressed. I have seen the traping the signal , but they give option only for traping the defined interrupt signals. But those does not help me here. (3 Replies)
Discussion started by: praveenbvarrier
3 Replies

8. Shell Programming and Scripting

Sequence number generation on a key column

Hi Folks, Can you help me with this issue: I have to generate the numbers say from 1001 for each record in a file based on a key field, the catch is the generated number should be unique based on key column. (EMP_NUMBER) Example: Input File: EMP_NUMBER EMP_NAME 8908 ... (6 Replies)
Discussion started by: sbasetty
6 Replies

9. UNIX for Dummies Questions & Answers

Capture an empty key press...

I am trying to test input from the user, if they press enter with out an Y or N. I have the characheter thing sorted but when it comes to a blank or empty key press I am having trouble. if ; then clear echo "Sorry, that is an invalid choice!" exit fi I am using a KSH script in... (3 Replies)
Discussion started by: jagannatha
3 Replies

10. Shell Programming and Scripting

Read line with a single key press...

I would really like to have a script that will accept the key press from the user with out having to press the enter key afterwards. i.e. echo "Press Y to print \c" read YesNo At this point the user has to press the enter key to continue. Is there a way to accept the key press from the... (3 Replies)
Discussion started by: jagannatha
3 Replies
Login or Register to Ask a Question