Reputation: 9437
I'm trying to check if the number of arguments in my bash script exceeds the number 2, and exit with a non-zero exit status, but I keep getting the following error :
unexpected token `newline', conditional binary operator expected
This is my code:
if [[ #$ > 2 ]]
then
echo "error" 1>&2
exit 1
fi
This is very simple, but I can't figure out why I get this error. Am I not supposed to use double brackets in the if statement?
Upvotes: 0
Views: 324
Reputation: 185530
if (( $# > 2 ))
then
echo "error" >&2
exit 1
fi
Check http://wiki.bash-hackers.org/syntax/arith_expr
Like greybot said on irc://irc.freenode.org/#bash : the tldp bash guide is outdated, and in some cases just plain wrong. There's a reason it isn't in the topic
Upvotes: 2
Reputation: 1
One way is to use arithmetic expansion:
if (( $# > 2 ))
then
echo "error" 1>&2
exit 1
fi
Upvotes: 0