1

I want to create a shell script that is should call one of two shell scripts based on the parameters passed.

For Example,

If -i & -s is exist in parameters list it should call script_1.sh
If -j is exist in parameters list it should call script_2.sh

script_call.sh should be like below,

while getopts :i:s:j OPTIONS
do
   case ${OPTIONS} in
      i)  INI_FILE=${OPTARG};;
      s)  INI_SECTION=${OPTARG};;
      #both -i & -s parameters passed, so call script_1.sh
      #script_1.sh -i ini_file.txt -s ini_section
      j)  JOB_KEY=${OPTARG};;
      #only -j parameter is passed, so call script_2.sh
      #script_2.sh -j 123
   esac
done
Inian
  • 80,270
  • 14
  • 142
  • 161
  • Does this answer your question? [How to call one shell script from another shell script?](https://stackoverflow.com/questions/8352851/how-to-call-one-shell-script-from-another-shell-script) – JohnnyJS Mar 31 '20 at 07:47
  • Thanks! JonnieJs. That question is about calling another plain shell script from current script but my question is calling shell script based on the parameters passed. – bala chandar Mar 31 '20 at 07:56
  • 1
    Don't add the call to run the script while parsing the args, but after the args processing. Because you need to handle cases when the order of flags is reversed, i.e. `-s` first and `-i` after that – Inian Mar 31 '20 at 07:56

1 Answers1

1

Change :i:s:j to i:s:j:, and use another case after the loop.

case ${INI_FILE+x},${INI_SECTION+x},${JOB_KEY+x} in
( x,x, ) script_1.sh -i "$INI_FILE" -s "$INI_SECTION" ;;
( ,,x  ) script_2.sh -j "$JOB_KEY" ;;
( *    ) : handle ambiguous parameters here
esac
oguz ismail
  • 1
  • 16
  • 47
  • 69