Argument check

All,

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.

Thanks,

Herb

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?

Close, try changing your if condition to the following:

if [[ "$1" != @("-e"|"-d") ]]; then
2 Likes

RudiC - My apologies, I'm writing in bash.

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'

Did I write it incorrectly?

TIA,

Herb

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 [[ "$1" != "-e" && "$1" != "-d" ]]; then
1 Like

The extended pattern became default behavior inside double square brackets in bash version 4.1, so I imagine OP's bash version is an older one.

Thanks RudiC. Yes my bash version is quite older: GNU bash, version 3.2.25(1).

I was able to get the desired behavior with your suggested modification.

I appreciate your help.

Herb

If you are doing parameter parsing you should consider using the bash getopts function:

while getopts :de opt
do
    case $opt in
        d) echo "You selected option d" ;;
        e) echo "You selected option e" ;;
        *) echo "Illegal option -$OPTARG" >&2
           exit 1
        ;;
    esac
done
shift $((OPTIND-1))

echo "The rest of the arguments are: $@"
2 Likes

With a recent bash or ksh you could also use:

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:

utility -d -e
utility -e -d
utility -de
utility -ed

Obviously this type of parsing can be done with if s and/or case s, but why reinvent the wheel.

All other observations aside, with regards to extended globbing, with your shell version, pilnet101's suggestion should work if you specify

shopt -s extglob

first...