bash script from docker does not work as expected if statement [duplicate] - bash

This question already has answers here:
Difference between sh and Bash
(11 answers)
Pattern matching in UNIX Case statement
(1 answer)
Closed 1 year ago.
I am using this image which has bash v4.3.48 and curl v7.56.1:
https://hub.docker.com/r/bizongroup/alpine-curl-bash/tags?page=1&ordering=last_updated
Inside the docker I write the following script:
email_dest="iz#gmail.com}}"
suffix="#gmail.com"
dm_to=${email_dest%"$suffix"}
if [[ $email_dest == *"#users.noreply.github.com"* ]]
then
echo "Email address is no reply. Please fix your email preferences in Github"
elif [[ $email_dest == *$suffix* ]]
then
curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello <#'"$dm_to"'>. '{{inputs.parameters.workflow_name}}' "}' https://hooks.slack.com/services/T01JNE5DXA7/B0246T84N75/hHDk7RUg2BWl2bYbPoN9r
else
echo "Email address is not of digibank domain!"
fi
If I run this script with bash command <script_name> it will work as expected (Run the curl command). But if I run it with sh command <script_name> it will not run the curl command:
/ # bash send-message.sh
ok/ #
/ # sh send-message.sh
Email address is not of digibank domain!
Any suggestion of what it could be? and what should be changed so it will work with sh?

That lies within the differences between bash and sh:
sh is POSIX compliant, whereas bash isn't (fully).
As a best practice you should always include a shebang:
#!/usr/bin/env bash
echo "this is going to run within bash"
With this you can now omit calling the script via bash myscript and just call it with ./myscript and it is always going to use bash (even if you are in a zsh, sh or whatever else).
However, if you truly want to have a script that runs with both sh and bash then you should rewrite your script to be plain sh compliant (i.e. POSIX).
TL;DR
Any suggestion of what it could be? and what should be changed so it will work with sh?
In your script you are using bash extensions such as [[ which is why it does not work with sh.
Checkout the links I posted above for more differences and how you can "convert" your bash script into a sh script.
The following site has a great summary on what to change in order to get your bash script working for dash which is an implementation of sh: http://mywiki.wooledge.org/Bashism
Furthermore, you can also check if any issues exist by using the following site: https://www.shellcheck.net/

Related

Definitively determine if currently running shell is bash or zsh

How can I definitively determine if the currently running shell is bash or zsh?
(being able to disambiguate between additional shells is a bonus, but only bash & zsh are 100% necessary)
I've seen a few ways to supposedly do this, but they all have problems (see below).
The best I can think of is to run some syntax that will work on one and not the other, and to then check the errors / outputs to see which shell is running. If this is the best solution, what command would be best for this test?
The simplest solution would be if every shell included a read-only parameter of the same name that identified the shell. If this exists, however, I haven't heard of it.
Non-definitive ways to determine the currently running shell:
# default shell, not current shell
basename "${SHELL}"
# current script rather than current shell
basename "${0}"
# BASH_VERSINFO could be defined in any shell, including zsh
if [ -z "${BASH_VERSINFO+x}" ]; then
echo 'zsh'
else
echo 'bash'
fi
# executable could have been renamed; ps isn't a builtin
shell_name="$(ps -o comm= -p $$)"
echo "${shell_name##*[[:cntrl:][:punct:][:space:]]}"
# scripts can be sourced / run by any shell regardless of shebang
# shebang parsing
On $ prompt, run:
echo $0
but you can't use $0 within a script, as $0 will become the script's name itself.
To find the current shell (let's say BASH) if shebang / magic number executable was #!/bin/bash within a script:
#!/bin/bash
echo "Script is: $0 running using $$ PID"
echo "Current shell used within the script is: `readlink /proc/$$/exe`"
script_shell="$(readlink /proc/$$/exe | sed "s/.*\///")"
echo -e "\nSHELL is = ${script_shell}\n"
if [[ "${script_shell}" == "bash" ]]
then
echo -e "\nI'm BASH\n"
fi
Outputs:
Script is: /tmp/2.sh running using 9808 PID
Current shell used within the script is: /usr/bin/bash
SHELL is = bash
I'm BASH
This will work, if shebang was: #!/bin/zsh (as well).
Then, you'll get the output for SHELL:
SHELL is = zsh
While there is no 100% foolproof way to achieve it, it might help to do a
echo $BASH_VERSION
echo $ZSH_VERSION
Both are shell variables (not environment variables), which are set by the respective shell. In the respective other shell, they are empty.
Of course, if someone on purpose creates a variable of this name, or exports such a variable and then creates a subshell of the different kind, i.e.
# We are in bash here
export BASH_VERSION
zsh # the subshell will see BASH_VERSION even though it is zsh
this approach will fail; but I think if someone is really doing such a thing, he wants to sabotage your code on purpose.
This should work for most Linux systems:
cat /proc/$$/comm
Quick and easy.
Working from comments by #ruakh & #oguzismail, I think I have a solution.
\shopt -u lastpipe 2> /dev/null
shell_name='bash'; : | shell_name='zsh'

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)

How to check the current shell and change it to bash via script?

#!/bin/bash
if [ ! -f readexportfile ]; then
echo "readexportfile does not exist"
exit 0
fi
The above is part of my script. When the current shell is /bin/csh my script fails with the following error:
If: Expression Syntax
Then: Command not found
If I run bash and then run my script, it runs fine(as expected).
So the question is: If there is any way that myscript can change the current shell and then interpretate rest of the code.
PS: If i keep bash in my script, it changes the current shell and rest of the code in script doesn't get executed.
The other replies are correct, however, to answer your question, this should do the trick:
[[ $(basename $SHELL) = 'bash' ]] || exec /bin/bash
The exec builtin replaces the current shell with the given command (in this case, /bin/bash).
You can use SHEBANG(#!) to overcome your issue.
In your code you are already using she-bang but make sure it is first and foremost line.
$ cat test.sh
#!/bin/bash
if [ ! -f readexportfile ]; then
echo "readexportfile does not exist"
exit 0
else
echo "No File"
fi
$ ./test.sh
readexportfile does not exist
$ echo $SHELL
/bin/tcsh
In the above code even though I am using CSH that code executed as we mentioned shebang in the code. In case if there is no shebang then it will take the help of shell in which you are already logged in.
In you case you also check the location of bash interpreter using
$ which bash
or
$ cat /etc/shells |grep bash

How can I resolve this error in shell scripting: "read: Illegal option -t"?

#!/bin/bash
echo -n "Hurry up and type something! > "
if read -t 10 response ; then
echo "Greate, you made it in time!"
else
echo "sorry, you are too slow!"
fi
I have written above code in terminal and got error "read: Illegal option -t".
Bash supports -t, so it looks like you're trying to execute it with sh or some other shell, which is odd, since you have the correct shebang.
Make sure you run it with ./script or path_to_script/script. If you just run it in the terminal, first start bash.
I had the same problem and then I figured out that I was using #!/bin/sh instead of #!/bin/bash. After changing the shebang everything worked as desired.
bash supports the -t option for the read builtin since version bash-2.04 (see ChangeLog), so either you are using an ancient version of bash (<= 2.03) or are not really running your script under bash.
Run bash --version to check the version and double-check that your shebang really looks like #!/bin/bash in your script.

read: Illegal option -d

Here is the offending part of my script:
read -d '' TEXT <<'EOF'
Some Multiline
text that
I would like
in
a
var
EOF
echo "$TEXT" > ~/some/file.txt
and the error:
read: 175: Illegal option -d
I use this read -d all over the place and it works fine. Not sure why its not happy now. I'm running the script on Ubuntu 10.10
Fixes? Workarounds?
If you run sh and then try that command, you get:
read: 1: Illegal option -d
If you do it while still in bash, it works fine.
I therefore deduce that your script is not running under bash.
Make sure that your script begins with the line:
#!/usr/bin/env bash
(or equivalent) so that the correct shell is running the script.
Alternatively, if you cannot do that (because the script is not a bash one), just be aware that -d is a bash feature and may not be available in other shells. In that case, you will need to find another way.
The -d option to read is a feature unique to bash, not part of the POSIX standard (which only specifies -r and -p options to read). When you run your script with sh on Ubuntu, it's getting run with dash, which is a POSIX shell, and not bash. If you want the script to run under bash then you should run it with bash, or give it a #!/bin/bash shebang. Otherwise, it should be expected to run under any POSIX sh.

Resources