Thursday 5 December 2019

bash - Shell script: unexpected operator for -ne

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.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...