LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-20-2009, 10:25 AM   #1
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Rep: Reputation: 15
Shell script goto?


I have several bash scripts I have made, and it's extremely inconvenient when there is a Y/N decision to be made, and if I make a typo or just hit enter it runs/messes up/exits the script. Is there a way to make it so "else" will go to the beginning of the question?

Example:

Do you like cheese? [Y/N]:
<read command is here>

If I press enter, or input something else and press enter the scripts messes up and exits instead of saying "invalid option"
 
Old 08-20-2009, 10:33 AM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Will you please show us your code so that we could suggest you something.
May be someone could suggest you something better IMHO.
 
Old 08-20-2009, 11:10 AM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Code:
while [ x$CHEESE != "xN" ] && [ x$CHEESE != "xY" ]
do
    echo 'Do you like cheese? [Y/N]:'
    read CHEESE
done
 
Old 08-20-2009, 11:45 AM   #4
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Original Poster
Rep: Reputation: 15
ilikejam likes cheese too? Thanks, it works great. One question though...
"x$CHEESE != "xN""

What do those "x"'s signify? Wouldn't it be looking for "xN", and not "N"? I don't think I've ever seen that in a script before...

Thanks!

PS: How would I make it so pressing enter on a user input field (that can be anything) doesn't kill my script?

Last edited by hawk__0; 08-20-2009 at 11:56 AM.
 
Old 08-20-2009, 12:34 PM   #5
dickgregory
Member
 
Registered: Oct 2002
Location: Houston
Distribution: Arch, PCLinuxOS, Mint
Posts: 257

Rep: Reputation: 34
The comparison is x$Cheese != ... (notice the x.

The x is to guarantee that you will not be comparing against a null value.

Without the x, if you hit enter without keying anything, the shell would expand it to:

while [ != "N" ] && [ != "Y" ]

Just like in algebra, you can modify both sides of the equation the same way without upsetting its equality, but in this case you have to do it before shell expansion.
 
Old 08-20-2009, 12:37 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by hawk__0 View Post
I don't think I've ever seen that in a script before...
It used to be common defensive programming, often used before [[ was added to augment [. It guards agains empty and unset variables.
 
Old 08-20-2009, 12:42 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ilikejam View Post
Code:
while [ x$CHEESE != "xN" ] && [ x$CHEESE != "xY" ]
do
    echo 'Do you like cheese? [Y/N]:'
    read CHEESE
done
Try (untested, but the idea is OK)
Code:
while read
do
    case $REPLY in
       y | Y | n | N )
           break
           ;;
       * )
           echo "Invalid answer '$REPLY'"
    esac
done
 
Old 08-20-2009, 05:50 PM   #8
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 111Reputation: 111
I was always taught and believe that break statements like that are bad for the readability of code.


Code:
EXIT="always initialize"
while [ "x$EXIT" != "xtrue" ]; do
echo -n "Make your selection (yes|NO): "
read SELECTION FOOBAR 
case $SELECTION in
  y*|Y*)
    echo "You said \"Yes\""
    EXIT=true
    ;;
  *)
    echo "I can't allow you to do that."
    EXIT=false
    ;;
esac
done
 
Old 08-20-2009, 06:22 PM   #9
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Quote:
I was always taught and believe that break statements like that are bad for the readability of code.
In a large/complex code block, maybe. For simple input validation like this, I'd go for brevity.

Dave
 
Old 08-21-2009, 01:46 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Luckily for me, when I did the (ksh) course many yrs ago, the told us to stick with double brackets for various reasons, including null args as mentioned. There's other info here for bash http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
 
  


Reply

Tags
bash, script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
goto/label command for scripting in bash shell terry.trent Linux - Software 3 07-09-2010 10:15 AM
goto in linux shell script sabliny LinuxQuestions.org Member Intro 1 10-13-2005 03:00 PM
can you use goto in bash script sabliny Programming 3 10-07-2005 05:54 PM
how to create a script that can goto different dir and also to run prompts ann124 Programming 5 11-29-2004 12:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration