if statement problem

hi all. i just have a very small problem. i have a menu of 7 choices. i want an if statement so that if the user chooses anything except inside the 1 to 7 range, i can handle the error for it.

i tried this:

if [ $choice -ne [1-7] ]
then
.......
fi

(but it dont work)

...any suggestions?

thanks all in advance

use :-

if [[ $choice -lt 1 || $choice -gt 7 ]]
then
.....
fi

cheers

Hi djt !

One of the reasons why your script does not work is that the left side MUST be in quotes, eg:

if [ "$number" = "1" ]; then
    echo "Number equals 1"
else
    echo "Number does not equal 1"
fi

Refer to this link for excellent explanation: http://linuxcommand.org/wss0100.php

Hope that was helpful :smiley:
Regards
Graham

Hi again djt !!

Try this one, this idea addresses the RH-side of the equation, namely: ranges of values:

read character
case $character in
            # Check for letters
    [a-z] | [A-Z] ) echo "Alpha range i[a-z]: You typed the letter $character"
            ;;
            # Check for digits
    [0-9] )     echo "Numeric range: 0-9: You typed the digit $character"
            ;;
            # Check for anything else
    * )         echo "You did not type a letter or a digit"
esac

Try this link for further explanations: http://linuxcommand.org/wss0120.php

Again, I hope this is usefull! :smiley:
Rergards
GrahamB

"-ne" is an operator to compare integer values (for being not equal). "[-7]" is a string, not an Int, this is why the test failed.

How to solve your task better has already been explained.

bakunin