Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm partway writing a script on Linux and I wanted to add an extra bit and ask the user whether they want to quit the program, can anyone help me in figuring out how I can do this?

share|improve this question
    
you can exit as "exit 0". –  alpertek Sep 29 '14 at 13:44
    
But how to I ask the user whether they want it to exit? –  Jas Sep 29 '14 at 13:46
4  
In whatever way you want. echo -n "Do you want to stop? "; read answer; if [ "$answer" = y ]; then exit 0; fi –  wurtel Sep 29 '14 at 13:47
1  
Is it that you want the user to be able to stop the script whenever he wants during the run, without having to ask for it? If so, the user can always do a CTRL-C that you can catch with trap to perform any housekeeping. –  unxnut Sep 29 '14 at 13:50
1  
Please edit your question and explain exactly what you need. Mention that you want to ask the user whether or not to exit for example. @wurtel you may as well make that into an answer. –  terdon Sep 29 '14 at 13:58

1 Answer 1

Try this:

read -p "Do you want to continue? (y/N) " ANS
    # if not, do something else, otherwise fall out the bottom
    [ "$ANS" = "y" -o "$ANS" = "Y" ] && {
            # commands here what to do if we stick around
            echo "OK, we will continue"
            # better put something else to do here or we'll fall out anyway.
            }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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