LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell script goto? (https://www.linuxquestions.org/questions/linux-general-1/shell-script-goto-749042/)

hawk__0 08-20-2009 10:25 AM

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"

vikas027 08-20-2009 10:33 AM

Will you please show us your code so that we could suggest you something.
May be someone could suggest you something better IMHO.

ilikejam 08-20-2009 11:10 AM

Code:

while [ x$CHEESE != "xN" ] && [ x$CHEESE != "xY" ]
do
    echo 'Do you like cheese? [Y/N]:'
    read CHEESE
done


hawk__0 08-20-2009 11:45 AM

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?

dickgregory 08-20-2009 12:34 PM

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.

catkin 08-20-2009 12:37 PM

Quote:

Originally Posted by hawk__0 (Post 3651090)
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.

catkin 08-20-2009 12:42 PM

Quote:

Originally Posted by ilikejam (Post 3651054)
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


lumak 08-20-2009 05:50 PM

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


ilikejam 08-20-2009 06:22 PM

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

chrism01 08-21-2009 01:46 AM

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


All times are GMT -5. The time now is 04:55 AM.