draca
draca

Reputation: 1399

unix shell script unexpected error

when I run the following script

#!/bin/sh
[ `whoami` == root ] || echo "must be run as root" 

I get the following error

./test.sh: 2: [: root: unexpected operator

How can I avoid that error?

Upvotes: 1

Views: 877

Answers (1)

avivr
avivr

Reputation: 1523

While it might seem like the problem is not quoting the word root, your script does run without error on my machine, even without quoting it. So it seems your error depends on the shell implementation.

The problem is that sh is implemented by different shells in different environments. The posix sh command doesn't support == (only =), and I think that's the error you're experiencing. See e.g. this answer.

Try changing the first line to #!/bin/bash to see if this is the case on your machine.

Upvotes: 6

Related Questions