Skip to main content
Post Closed as "Duplicate" by Kusalananda, ilkkachu, jimmij, Jeff Schaller, Stephen Harris
added 1 character in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

I am writing a bash shell script at the moment and one of the specifications is that I need to print an error message if the first argument, $1, is not -e or -d, this is what I have done:

if [ "$1" != "-e" ] || [ "$1"$1" != "-d" ]; then
        echo "Error: First argument must be either -e or -d"
        exit 1
fi

this does not seem to work however, as even if i used -e or -d for the first argument, this error message is still printed. What am I doing wrong and how do I fix this?

I am writing a bash shell script at the moment and one of the specifications is that I need to print an error message if the first argument, $1, is not -e or -d, this is what I have done:

if [ "$1" != "-e" ] || [ "$1 != "-d" ]; then
        echo "Error: First argument must be either -e or -d"
        exit 1
fi

this does not seem to work however, as even if i used -e or -d for the first argument, this error message is still printed. What am I doing wrong and how do I fix this?

I am writing a bash shell script at the moment and one of the specifications is that I need to print an error message if the first argument, $1, is not -e or -d, this is what I have done:

if [ "$1" != "-e" ] || [ "$1" != "-d" ]; then
        echo "Error: First argument must be either -e or -d"
        exit 1
fi

this does not seem to work however, as even if i used -e or -d for the first argument, this error message is still printed. What am I doing wrong and how do I fix this?

Source Link

Checking first argument of a script if it is -e or -d

I am writing a bash shell script at the moment and one of the specifications is that I need to print an error message if the first argument, $1, is not -e or -d, this is what I have done:

if [ "$1" != "-e" ] || [ "$1 != "-d" ]; then
        echo "Error: First argument must be either -e or -d"
        exit 1
fi

this does not seem to work however, as even if i used -e or -d for the first argument, this error message is still printed. What am I doing wrong and how do I fix this?