0

My script needs two arguments. I want to hide the error message if someone calls the script with

script.sh --help

so I tired this:

if [ $# -ne 2 ] ; then
  if [ "$1" -ne "--help" ]; then
    echo "ERROR: wrong number of parameters"
    echo
  fi
  echo "Syntax: $0 foo bar
  exit 1
fi

But I get the error

script.sh: line 10: [: --help: integer expression expected

What is wrong?

1
  • You are using -ne, which is for integer comparison. Just use [ ! "$1" == "--help" ] instead Commented Jul 30, 2014 at 13:19

1 Answer 1

1

the parameter -ne is only valid for numbers, you have to use != for string comparism.

This works:

if [ $# -ne 2 ] ; then
  if [ "$1" != "--help" ]; then
    echo "ERROR: wrong number of parameters"
    echo
  fi
  echo "Syntax: $0 foo bar
  exit 1
fi

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.