Maverick
Maverick

Reputation: 215

Why am I getting "non -option ARGV element: XXXX" in shell script?

I'm trying to make a file for login into cyberoam client in Linux.

crclient is a file which takes different arguments. ./crclient -u username for login and ./crclient -l for logout. The shell script which I have written is working fine for login but when I'm trying to logout as ./crclient -l terminal is showing non-option ARGV-elements: u09me215.

#!/bin/sh
cd Downloads/crclient
if [ $1=="-u" ]
then
./crclient $1 u09me215
else
./crclient $1
fi

Now script is running correctly after replacing if [ $1=="-u" ] with if [ $1 = "-u" ] as @devnull pointed. But what is difference between "==" and "=" other than one is used for comparisons and other one is used for value assigning. And why == was working good for -u and not for -l.

Upvotes: 0

Views: 1712

Answers (1)

chepner
chepner

Reputation: 530960

Without spaces, $1==-u is treated as a single string, not a comparison expression. Since a non-empty string always evaluates as true in a test command, you end up running

./crclient -l u09me215

which apparently leads to the error you see.

On a more subtle note, [ $1 == "-u" ] is technically not allowed, as the test command (as specified by POSIX) does not use == for string comparison. However, if your /bin/sh is actually bash, then bash strays a bit from the standard and allows it. You should use either

[ "$1" = "-u" ]    # Note the quotes around $1

or

[[ "$1" == "-u" ]]   # A single = would be fine, and quoting $1 is optional but recommended.

Upvotes: 1

Related Questions