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
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.