Shell script: unexpected operator for -ne [duplicate] - bash

This question already has answers here:
Difference between sh and Bash
(11 answers)
String comparison in bash. [[: not found
(9 answers)
Closed 4 years ago.
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.

Related

`shopt -s inherit_errexit` has no effect in declare command [duplicate]

This question already has answers here:
Exit code of variable assignment to command substitution in Bash
(5 answers)
Closed 1 year ago.
#!/bin/bash
set -e
set -o pipefail
shopt -s inherit_errexit
declare _x=`command-with-error`
echo "_x=${_x}"
Run the script shows:
bash xx.sh
xx.sh: line 6: command-with-error: command not found
_x=
Apparently line 6 did not exit the shell. What option should I use to make the script exit when the subshell command on declare line fails?
The successful exit status of declare is overriding the unsuccessful exit status of command-with-error.
Break it into two separate commands:
declare _x
_x=$(command-with-error)
...as you can see running correctly (which is to say, without writing anything to stdout) at https://ideone.com/TGyFCZ

alias not recognized if alias command is piped to bash [duplicate]

This question already has answers here:
bash: unable to set and use alias in the same line
(2 answers)
Closed 3 years ago.
I would like to get aliases to work in non-interactive bash. I run the following command :
bash -c "alias toto=ls; shopt -s expand_aliases; alias toto=ls; toto"
and I get the following :
bash: toto : commande not found
What am I doing wrong ?
From man bash:
Aliases are expanded when a command is
read, not when it is executed. Therefore, an alias definition
appearing on the same line as another command does not take
effect until the next line of input is read.
That means, even in an interactive shell,
alias toto=ls; toto
wouldn't work. There must be a line break between alias definition and call. So,
bash -c 'shopt -s expand_aliases; alias toto=ls
toto'
should work.

Read command shows error as illegal option [duplicate]

This question already has answers here:
read: Illegal option -d
(2 answers)
Closed 5 years ago.
The following is my code
Read file
Count=0
While read -n1 c
Do
Case $c in
.
.
.
.
Esac
Done < $file
Echo"$count"
When I run this code, it shows the error as
read: Illegal option -n
I'm just started learning shell programming.So please help me fix this code
-n is not an option for read in standard Unix sh and (some of) its variants.
read -n runs well on bash, zsh and ksh93, so you may want to select one of them instead of sh or dash (Debian sh), probably by adding a shebang line:
#! /bin/bash
Or run explicitly with bash:
bash foo.sh

Bash judgement gets the unexpected result [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 5 years ago.
This is the code of the my shell script:
#! /bin/bash
if ["$SHELL" = "/bin/bash"];then
echo "this is bash"
elif ["$SHELL" = "aa"];then
echo "this is aa"
else
echo "this is not /bin/bash, but $SHELL"
fi
why I execute the test_bash_03 script file gets the else result? shouldn't it be:this is bash ?
aircraftdeMacBook-Pro:bash_demo ldl$ ./test_bash_03
./test_bash_03: line 3: [/bin/bash: No such file or directory
./test_bash_03: line 5: [/bin/bash: No such file or directory
this is not /bin/bash, but /bin/bash
And I echo the $SHELL I also get the /bin/bash
aircraftdeMacBook-Pro:bash_demo ldl$ echo $SHELL
/bin/bash
You are missing a space after [ and before ].
The bash tries to execute a command named [/bin/bash instead of [ (which is test), then doesn't find that and has an exit code of 1 (false). So you end up in the else case.

BASH - getting UID on shell script does not work [duplicate]

This question already has an answer here:
Blank first line of shell script: explain behavior of UID variable
(1 answer)
Closed 6 years ago.
Hi I have a question about bash.
and I'm new to it.
I made a file named "test.sh" and its contents is
#!/bin/bash
set -x
echo $UID
echo "$UID"
echo "$(id -u)"
and the result is blank!!
nothing shows up
However, when i just type "echo $UID" on terminal
it shows "1011"
is there anything i missed for bash?
Please help
UPDATED
bash version is 4.3.11 and I typed "sh test.sh" to execute.
and the result is
+ echo
+ echo
+ id -u
+ echo 1011
1011
thanks!
$UID is a Bash variable that is not set under sh, that may be why it outputs blank lines.
Try bash test.sh or make your script executable with chmod u+x test.sh, the program defined in shebang will then be used (/bin/bash)

Resources