Bash脚本,带有运行另一个脚本的选项

时间:2018-07-12 11:12:57

标签: bash

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

LIMIT 500

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

类似

buildapp.sh -build1
buildapp.sh -build2 
etc

2 个答案:

答案 0 :(得分:1)

我认为这就是您想要的:

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(请参见An example of how to use getopts in bash

答案 1 :(得分:1)

解决方案

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

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

解决方案(扩展)

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

扩展了上述版本,因此,如果参数前缀为./scriptXXX.sh

,则仅运行-build

相关问题