使用sh运行bash脚本 [英] Run bash script with sh
问题描述
我有bash脚本,它需要bash.
I have bash script and it requires bash.
另一个人尝试用
sh script_name.sh
它失败了,因为sh是他分布中破折号的符号链接.
And it fails because sh is symbolic link to dash in his distribution.
$ ls -la /bin/sh
lrwxrwxrwx 1 root root 4 Aug 25 16:06 /bin/sh -> dash
我有一个使用包装脚本的想法:
I have an idea to use wrapper script:
#!/bin/sh
bash script_name.sh
目标是在具有符号链接到破折号的系统中使用bash通过sh运行.sh脚本.
The goal is to run .sh script by sh with bash in system having symbolic link to dash.
推荐答案
Well, usually you use the shebang to tell the shell to use the correct interpreter:
#!/bin/bash
# your script here
您必须将脚本设置为可执行的:
You have to set the script to be executable:
chmod +x my_script.sh
然后让用户以以下内容开始:
And let the user start it with:
./my_script.sh
似乎比使用包装脚本更简单.
It seems simple than to use a wrapper script.
即使用户使用sh/dash或任何类似sh的解释器,也可以使用jbr test使用bash运行脚本:
You can use jbr test to run your script with bash even if the user use sh/dash or any sh like interpreter:
#!/bin/bash
if [ -z "$BASH_VERSION" ]
then
exec bash "$0" "$@"
fi
# Your script here
这样,它就可以正确地与以下任何一个一起工作:
This way it correctly works with either :
sh ./my_script.sh
# or
bash ./my_script.sh
# or
./my_script.sh
这篇关于使用sh运行bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!