0

I have a shell script where I want to check if it's being run by root. If it's not then I set the variable SUDO='sudo' to prepend to subsequent commands. That way the user gets one nice permission denied error message from the shell and if they're running with sudo, it properly does its business. However, for the following code I get:

myscript.sh: [: -ne: unexpected operator

Code is:

#!/bin/sh

SUDO=""
if [ $EUID -ne 0 ]; then
    SUDO='sudo'
fi

I am running it like: sh myscript.sh as this is not a bash script. This is not an issue of sh vs bash afaik. The shebang says /bin/sh and I'm running it with sh.

xendi
  • 2,332
  • 5
  • 40
  • 64
  • Can't reproduce. Post more code. – Elliott Frisch Jul 15 '18 at 15:07
  • That's all the code that is required. That alone in a .sh file (With hashbang of course) produces the error for me. – xendi Jul 15 '18 at 15:09
  • What do you mean, "*With hashbang of course*"? Show your actual code. That is, post a [mcve]. – melpomene Jul 15 '18 at 15:09
  • Are you running with `sh` or `bash`? Take a look at this post; I don't know Bash, but the error looks the same. https://unix.stackexchange.com/questions/390131/receiving-a-permission-denied-message-when-checking-for-regex-through-shell-sc – JohnLBevan Jul 15 '18 at 15:09
  • Edited to show full code to reproduce. – xendi Jul 15 '18 at 15:10
  • What is stored in `EUID`? Is it a global environment variable? – kiner_shah Jul 15 '18 at 15:10
  • It's still missing the part where you run this file. How are you running the script? – melpomene Jul 15 '18 at 15:10
  • edited for that – xendi Jul 15 '18 at 15:12
  • Have you tried running it with bash? – melpomene Jul 15 '18 at 15:12
  • hmm. When I run with bash it does not have an error. What's going on? – xendi Jul 15 '18 at 15:13
  • 3
    Your `sh` and `bash` are different shells. Bash supports `[[`; your `sh` does not. – melpomene Jul 15 '18 at 15:14
  • Oh. I know they're different but didn't know about the support. Care to answer? – xendi Jul 15 '18 at 15:15
  • Using `sh` to start scripts is bad habit. Consider putting a line like `case $BASH_VERSION in ''|[12].*) echo "Requires bash 3.x or newer" >&2; exit 1;; esac` just after your script's shebang to stop people from doing it. – Charles Duffy Jul 15 '18 at 16:04
  • 1
    ...if, instead of using `sh yourscript` you run `./yourscript`, *the script itself* will be able to choose its interpreter, so you can be assured that it runs in the same interpreter it was developed and tested against. This also means you can rewrite from `sh` to, say, Python or C, and not need to tell your users to change how they invoke a program. Related: [Commandname Extensions Considered Harmful](https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/) – Charles Duffy Jul 15 '18 at 16:04
  • well, I've changed it to `/bin/sh` now. I don't want people to have to set +x before running it. Still the same error for a simple if statement. – xendi Jul 15 '18 at 16:15
  • Yes, `[[` is not part of POSIX sh, so you can never be guaranteed it will work with `/bin/sh`. It's not `if` that's the problem, it's `[[`. – Charles Duffy Jul 15 '18 at 16:20
  • As I've said above, `sh` does not support `[[`, so of course you're getting an error for that. I'm not sure what's unclear. – melpomene Jul 15 '18 at 16:20
  • `[ "$EUID" -ne 0 ]`, by contrast, will be fine. – Charles Duffy Jul 15 '18 at 16:21
  • Apparently you somehow got a file named `[[` in your PATH, but it's not executable, so `sh` tries to use it (since it has no builtin implementation), but can't. Frankly, that file shouldn't be there in the first place -- the whole thing that makes `[[` special is that it's shell syntax, not a regular command, so any external implementation would be inherently buggy. – Charles Duffy Jul 15 '18 at 16:23
  • ...now you edited this into a *completely different question*. And you didn't pay attention to the quotes. I made it `"$EUID"`, not `$EUID`, for a reason: If your `/bin/sh` doesn't populated a variable named `EUID`, if you use quotes you'll get a error message that makes that more clear. – Charles Duffy Jul 15 '18 at 16:23
  • If I add the quotes I get `[: Illegal number:` – xendi Jul 15 '18 at 16:24
  • 1
    Right. Meaning that you have no EUID variable. Because bash populates that and you aren't using bash. That's a more useful error message than one about `-ne` being an unexpected operator. – Charles Duffy Jul 15 '18 at 16:24
  • Except i do have it. – xendi Jul 15 '18 at 16:25
  • Not in `/bin/sh` you don't. – Charles Duffy Jul 15 '18 at 16:25
  • Run `sh -c 'echo $EUID'`, with exactly that quoting. – Charles Duffy Jul 15 '18 at 16:25
  • gah. can echo and return it back or something? – xendi Jul 15 '18 at 16:26
  • nevermind. that didn't make sense. I need it – xendi Jul 15 '18 at 16:27
  • You could collect it out-of-process with `$(id -u)` if you only need to run on GNU systems. Or you could use bash. Why don't you do that? – Charles Duffy Jul 15 '18 at 16:27
  • Will just use bash – xendi Jul 15 '18 at 16:27

0 Answers0