I wrote a shell script that checks the command line arguments for the "-i" flag.
I designed it to run from inside my gui application.
I tested my script from the terminal and it runs perfectly, does just what I want and expect. So I ran my gui (which needs superuser) and my gui would not run correctly. So I ran my gui from the terminal to see its output and saw a plethora of errors from my script (output at bottom of post).
I couldn't find a problem with my script, so I tried breaking it down to diagnose the problem. I found that when I ran my script as superuser it returned errors. So I tried just running the error line of my script as superuser and it returned the same error. And I can not figure out why.

The problem line is:
if [ "$x" == "-i" ]

To reproduce the error, run:
[ "-i" == "-i" ] ; echo $?
and
sudo [ "-i" == "-i" ] ; echo $?
the second will produce an error.

[: 34: ==: unexpected operator
[: 34: -i: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: -i: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator
[: 34: ==: unexpected operator

I just cant figure it out

Recommended Answers

All 2 Replies

Actually do sudo test .... rather than sudo [ .... ]

Oh yes. I am sorry. I forgot to mention that I did try that (sudo test) and the result was identical.
I found the solution by the way. However I am still puzzled.
It seems that == is accepted and works correctly as a normal user, but as superuser, only = works and == returns an error.
My fault for using c++ syntax instead of bash. However, I am very puzzled as to why this only returns an error when using sudo. Also, I logged in as root and no errors were returned. So this only occurs when using sudo. Why does this happen, I don't know enough about sudo to understand this issue, but I would love to learn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.