quit any time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting quit any time
# 1  
Old 02-16-2008
quit any time

how can i read input to quit any time, for instance "type q to quit"

I have a script like this

echo "The first choice"
read firstChoice
echo "The second choice"
read secondChoice

Looking for a code to quit any time by pressing q to quit

any help would be appreciated

thanks
# 2  
Old 02-16-2008
something like this,

Code:
read var
if [ $var = "q" ]
then
echo "yes... quitting"
exit 1
fi

# 3  
Old 02-16-2008
Quote:
Originally Posted by matrixmadhan
something like this,

Code:
read var
if [ $var = "q" ]
then
echo "yes... quitting"
exit 1
fi

Like this

Code:
loop=1
while [ loop -eq "1" ]
   echo "Type F for first choice"
   read firstChoice
   echo "Type S for second choice"
   read secondChoice
   if [ "$firstChoice" = "F" ]; then
         echo "This is the first choice"
   elif [ "$secondChoice" = "S" ]; then
         echo "This is the second choice"
   else
         echo "no choice"
         loop=0
         exit 1   
   fi

  how do i catch the keyboard input for $var to quit any time? 
  if [ $var = "q" ]; then
      echo "yes... quitting"
    exit 1
   fi
done

# 4  
Old 02-16-2008
you need to check the input each and every time

Code:
check() {
  if [ $1 = "q" ]; then
      echo "yes... quitting"
    exit 1
   fi
 }

loop=1
while [ loop -eq "1" ]
   echo "Type F for first choice"
   read firstChoice
   #
   check firstChoice
   #
   echo "Type S for second choice"
   read secondChoice
   #
   check secondChoice
   #
   if [ "$firstChoice" = "F" ]; then
         echo "This is the first choice"
   elif [ "$secondChoice" = "S" ]; then
         echo "This is the second choice"
   else
         echo "no choice"
         loop=0
         exit 1   
   fi
done

# 5  
Old 02-16-2008
What do you mean by "any time"? Do you mean any time the script prompts you for input, or just whenever you press q? If you mean every time the script asks you for input, you could write a function like:
Code:
QUIT () {
            if [ $firstChoice = "q" ] || [ $secondChoice = "q" ]; then
              exit 1
            fi
             }

Then just call the function right after very read call. like:
Code:
loop=1
while [ loop -eq "1" ]
   echo "Type F for first choice"
   read firstChoice
QUIT
   echo "Type S for second choice"
   read secondChoice
QUIT
   if [ "$firstChoice" = "F" ]; then
         echo "This is the first choice"
   elif [ "$secondChoice" = "S" ]; then
         echo "This is the second choice"
   else
         echo "no choice"
         loop=0
         exit 1   
   fi

# 6  
Old 02-16-2008
Quote:
Originally Posted by System Shock
What do you mean by "any time"? Do you mean any time the script prompts you for input, or just whenever you press q?
i mean whenever q is pressed

I've tried the Quit function that did it for me.

I thank you all for all your quick response
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. OS X (Apple)

Are you sure you want to quit Safari?

Hmmm. I cannot figure out where to disable this warning message in Safari. Google says to disable something in Safari Tabs preferences but my Macs do not have that option in Mojave. Anyone know how to disable the following so when I quit Safari it simply quits without the "freeze the... (12 Replies)
Discussion started by: Neo
12 Replies

2. Shell Programming and Scripting

Need to quit out of for loop.

Hi, Here is my code to send read.txt to three servers. col="prd167.mybank.com prd168.mybank.com bsprd169.mybank.com" set -A look $col for IndixList in ${look}; do scp /tmp/read.txt admin@$IndixList:/tmp done This works and all the 3 servers gets the read.txt file. However,... (8 Replies)
Discussion started by: mohtashims
8 Replies

3. Shell Programming and Scripting

Quit FTP issue

Hey, same problem. i want to check if files have been successfuly transferred after getting them back, but after I quit the ftp, nothing gets executed. Difference from original post is that I get 226 and 221 feedbacks from the server. 226 Transfer complete. 2409 bytes received in 0.0086... (5 Replies)
Discussion started by: fpflug
5 Replies

4. Shell Programming and Scripting

if no file then quit

I have a script that run several subscripts. I need to find out how to do two things. First I would like to check for a file and if that file is not there I want to quit the entire script without running the rest of the script which contain subscripts. If the file is there, I want it to continue... (1 Reply)
Discussion started by: libertyforall
1 Replies

5. UNIX for Dummies Questions & Answers

Enter q for quit not working

I'm using a while loop with an if statement. When the user choses a number, it will display a list of files. That works, the problem is q for quit won't exit script. How can I fix this? (10 Replies)
Discussion started by: smiley76112
10 Replies

6. Shell Programming and Scripting

How to quit from a script?

hi all, I am facing problem in shell scripting while using exit command, when ever i run a file using . ./<filename>, when i run the sae script as sh <filename> the script does not close the windows. since my script has function calls i have to use . ./ <filename>. Could any one tell me... (8 Replies)
Discussion started by: caro
8 Replies

7. Shell Programming and Scripting

Quit Function Issues

Hi everyone, I am writing a small menu driven program and have come across a problem i am having trouble solving. I am using Windows XP and i am developing this in the Unix Bash shell. I am trying to make it possible to exit the program from each of the five main interfaces. The code is as... (4 Replies)
Discussion started by: warlock129
4 Replies

8. UNIX for Dummies Questions & Answers

how to quit from glance

hi, if i am in glance, how do i exit? thanks (2 Replies)
Discussion started by: yls177
2 Replies
Login or Register to Ask a Question