0

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
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Ken Doe
  • 11
  • 1
    Kindly include more information. The question is not clear. Is it you are looking for a conditional statement like if, if else etc? – Chandan Kumar Jul 12 '18 at 11:20
  • 1
    If you check the options you're passed, you can do whatever actions you'd like based on them. What are you having trouble with so far? – Eric Renouf Jul 12 '18 at 11:22

2 Answers2

1

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)

NetMonk
  • 55
  • 6
1

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

Rene Knop
  • 1,788
  • 3
  • 15
  • 27