来自 docker 的 bash 脚本无法按预期的 if 语句运行
bash script from docker does not work as expected if statement
我正在使用这张图片,其中包含 bash v4.3.48 和 curl v7.56.1:
https://hub.docker.com/r/bizongroup/alpine-curl-bash/tags?page=1&ordering=last_updated
在docker里面我写了下面的脚本:
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
如果我 运行 此脚本使用 bash 命令 它将按预期工作(运行 curl 命令)。但是,如果我 运行 使用 sh 命令 它不会 运行 curl 命令:
/ # bash send-message.sh
ok/ #
/ # sh send-message.sh
Email address is not of digibank domain!
有什么建议吗?应该更改哪些内容才能与 sh 一起使用?
位于 differences between bash
and sh
内:
sh
符合 POSIX,而 bash
不(完全)。
作为最佳实践,您应该始终包含 shebang:
#!/usr/bin/env bash
echo "this is going to run within bash"
有了这个,你现在可以省略通过 bash myscript
调用脚本,只需使用 ./myscript
调用它,它总是会使用 bash (即使你在 zsh
、sh
或其他任何内容)。
但是,如果你真的想要一个同时运行 sh
和 bash
的脚本,那么你应该重写你的脚本以使其与 sh
兼容(即 POSIX).
TL;DR
Any suggestion of what it could be? and what should be changed so it will work with sh?
在您的脚本中,您正在使用 bash
扩展名,例如 [[
,这就是为什么它不适用于 sh
。
查看我上面发布的链接以了解更多差异以及如何将 bash
脚本“转换”为 sh
脚本。
以下站点对要更改的内容进行了很好的总结,以便让您的 bash
脚本适用于 dash
,这是 sh
的一个实现:http://mywiki.wooledge.org/Bashism
此外,您还可以使用以下站点检查是否存在任何问题:https://www.shellcheck.net/
我正在使用这张图片,其中包含 bash v4.3.48 和 curl v7.56.1:
https://hub.docker.com/r/bizongroup/alpine-curl-bash/tags?page=1&ordering=last_updated
在docker里面我写了下面的脚本:
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
如果我 运行 此脚本使用 bash 命令
/ # bash send-message.sh
ok/ #
/ # sh send-message.sh
Email address is not of digibank domain!
有什么建议吗?应该更改哪些内容才能与 sh 一起使用?
位于 differences between bash
and sh
内:
sh
符合 POSIX,而 bash
不(完全)。
作为最佳实践,您应该始终包含 shebang:
#!/usr/bin/env bash
echo "this is going to run within bash"
有了这个,你现在可以省略通过 bash myscript
调用脚本,只需使用 ./myscript
调用它,它总是会使用 bash (即使你在 zsh
、sh
或其他任何内容)。
但是,如果你真的想要一个同时运行 sh
和 bash
的脚本,那么你应该重写你的脚本以使其与 sh
兼容(即 POSIX).
TL;DR
Any suggestion of what it could be? and what should be changed so it will work with sh?
在您的脚本中,您正在使用 bash
扩展名,例如 [[
,这就是为什么它不适用于 sh
。
查看我上面发布的链接以了解更多差异以及如何将 bash
脚本“转换”为 sh
脚本。
以下站点对要更改的内容进行了很好的总结,以便让您的 bash
脚本适用于 dash
,这是 sh
的一个实现:http://mywiki.wooledge.org/Bashism
此外,您还可以使用以下站点检查是否存在任何问题:https://www.shellcheck.net/