I wish to create a simple bash script.
buildapp.sh -build1
buildapp.sh -build2
etc
the option build1/2/3/ etc call an external script depending on option.
So something like
buildapp.sh -build1 → script1.sh
buildapp.sh -build2 → script2.sh
I wish to create a simple bash script.
buildapp.sh -build1
buildapp.sh -build2
etc
the option build1/2/3/ etc call an external script depending on option.
So something like
buildapp.sh -build1 → script1.sh
buildapp.sh -build2 → script2.sh
I think this is what you are looking for:
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
Another option is to use getops (see An example of how to use getopts in bash)
Solution
#!/bin/bash
./script${1//[!0-9]/}.sh # './' is the path to scriptX.sh, you may need to adjust it
A very tiny solution, that works with every number, by simply referencing the numeric argument suffix. E.g. it calls ./script123.sh
with -build123
.
Solution (extended)
#!/bin/bash
if [[ '-build' == "${1//[0-9]/}" ]]
then
./script${1//[!0-9]/}.sh
fi
Extends the above version, so that it only runs ./scriptXXX.sh
, if the argument prefix is -build