Bash脚本,其中包含运行另一个脚本的选项



我想创建一个简单的bash脚本。

buildapp.sh -build1
buildapp.sh -build2 
etc

选项build1/2/3/etc根据选项调用外部脚本。

所以类似的东西

buildapp.sh -build1 → script1.sh
buildapp.sh -build2 → script2.sh

我想这就是您想要的:

if [ "$1" = "-build1" ]; then
path/to/script1.sh
elif [ "$1" = "-build2" ]; then
path/to/script2.sh
elif [ "$1" = "-build3" ]; then
path/to/script3.sh
else
echo "Incorrect parameter"
fi

另一个选项是使用getops(请参阅如何在bash中使用getopts的示例(

解决方案

#!/bin/bash
./script${1//[!0-9]/}.sh # './' is the path to scriptX.sh, you may need to adjust it

一个非常小的解决方案,可以通过简单地引用数字参数后缀来处理每个数字。例如,它用-build123调用./script123.sh

解决方案(扩展(

#!/bin/bash
if [[ '-build' == "${1//[0-9]/}" ]]
then
./script${1//[!0-9]/}.sh
fi

扩展上述版本,使其仅运行./scriptXXX.sh,如果参数前缀为-build

相关内容

最新更新