0

I have this simple script, which wouldn't run because of the line with if [ ... ] Could anyone tell me what is wrong with this?

#! /bin/sh
if [ $# -ne 2 AND $# -ne 3 ]
then
    echo "Usage $0 <input> <output> [<comment>]"
    exit 1
fi;

Thanks!

1
  • If it is an error to call the script with the wrong number of arguments, then your code is correct to exit with a non-zero value. However, the error message belongs on stderr. eg echo "..." >&2 Commented Oct 5, 2012 at 1:41

1 Answer 1

2

Try the following :

#! /bin/sh
if [ $# -ne 2 -a $# -ne 3 ]
then
    echo >&2 "Usage $0 <input> <output> [<comment>]"
    exit 1
fi

Or :

#! /bin/sh
if [ $# -ne 2 ] && [ $# -ne 3 ]
then
    echo >&2 "Usage $0 <input> <output> [<comment>]"
    exit 1
fi

If you'd like to use bash :

#! /bin/bash
if [[ $# -ne 2 && $# -ne 3 ]]
then
    echo >&2 "Usage $0 <input> <output> [<comment>]"
    exit 1
fi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.