I'm writing an argument checking block and running into an error. I want to confirm that $1 is one of two values. Here is what I have:
if [[ "$1" != ("-e" || "-d") ]]; then
echo -e "\nPlease check your first augument. You used \"$1\"
which is not recognized. Please see usage:"
usage
exit 1
else
echo -e "\narg 1 is good to go.."
fi
Can anyone point me in the right direction? Any help is greatly appreciated.
The correct syntax depends heavily on the shell that you are using. Please post. What would you conclude from the error message that you certainly got but did not post either?
pilnet101 - I made your suggested modification as:
if [[ "$1" != @("-e"|"-d") ]]; then
echo -e "\nPlease check your first augument. You used \"$1\"
which is not recognized. Please see usage:"
usage
exit 1
else
echo -e "\narg 1 is good to go.."
fi
I receive the following error:
syntax error in conditional expression: unexpected token `('
syntax error near `@("'
`if [[ "$1" != @("-e"|"-d") ]]; then'
Surprisingly enough, pilnet101's proposal worked in my GNU bash, version 4.3.11(1) . I'm glad I learned something today (although a bit disappointed as I didn't find that feature in bash's man page)!
As your bash doesn't like that construct, try e.g. (amongst other options):
if [[ "$x" =~ ^-[de]$ ]]
then echo match
else echo no match
fi
Note, however, that if you're using code like this to parse command line options, I strongly encourage you to use getopts instead. It makes it much easier to behave like standard utilities where you can specify the two options -d and -e with any of the following four equivalent command lines: