1

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?

Inian
  • 80,270
  • 14
  • 142
  • 161
jrz
  • 1,213
  • 4
  • 20
  • 54
  • Please add a shebang and then paste your script at http://www.shellcheck.net/ – Cyrus Jun 13 '21 at 10:04
  • 2
    Are you asking how to remove the bashisms from the script, or how to get sh to execute bash when running this script? – BMitch Jun 13 '21 at 10:13
  • 2
    Probably not related to your current problem but: are you 100% sure you want to use an alpine based image provided by a github group that only produced 2 images and has not updated them once in the last 4 years, knowing you can easilly rebuild that yourself from the official and up-do-date alpine image by just running their [3 lines Dockerfile](https://github.com/bizongroup/alpine-curl-bash/blob/master/Dockerfile)? – Zeitounator Jun 13 '21 at 10:15
  • @BMitch I am asking how to get sh to run bash when running this script. Or simpler, when do I need to do in order to run this script as: sh send-message.sh so it will enter the expected "if" and run the curl command. – jrz Jun 13 '21 at 10:22

1 Answers1

3

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/

F1ko
  • 3,326
  • 1
  • 9
  • 24
  • Thanks for the elaborated answer! I added #!usr/bin/env bash and it didnt work. I am struggling with rewriting this script to run with sh. Any chances you can help here? – jrz Jun 13 '21 at 10:40
  • @I.zv : As I mentioned start rewriting the script by omitting `bash` extensions. You can check your `sh` script on the following site to get help on what you would need to rewrite: https://www.shellcheck.net/ Start by using only single (`[`) instead of double brackets (`[[`) within your script (see here for the effect: https://stackoverflow.com/questions/13542832/difference-between-single-and-double-square-brackets-in-bash). That should be sufficient enough to fix your if statements once you remove globs (`*`) and replace `==` with `=` as well. – F1ko Jun 13 '21 at 13:26
  • You can use the following site for a list of workarounds: http://mywiki.wooledge.org/Bashism – F1ko Jun 13 '21 at 13:29
  • What can I do inside the if statement? I MUST check "if string ends with..." I see it is not possible in shell? – jrz Jun 13 '21 at 14:31
  • @I.zv It's completely possible; just [use a `case` statement](https://stackoverflow.com/questions/42813783/pattern-matching-in-unix-case-statement). – Gordon Davisson Jun 13 '21 at 17:18