0

I am trying to write the input based bash script which will trigger the respective app build script. And if the input is "all" then all apps will get called. I am facing issue here, if entered as "all" then only app_1 and app_2 are getting called. what could be the issue,

if [ "${_BUILD_MODE}" = "all" ] || [ "${_BUILD_MODE}" = "app_1" ]
then 
   echo "BUILD_STAGE APP_1 build started"
fi
if [ "${_BUILD_MODE}" = "all" ] || [ "${_BUILD_MODE}" = "app_2" ]
then 
   echo "BUILD_STAGE APP_2 build started"
fi
if [ "${_BUILD_MODE}" = "all" ] || [ "${_BUILD_MODE}" = "app_3" ]
then 
   echo "BUILD_STAGE APP_3 build started"
fi
if [ "${_BUILD_MODE}" = "all" ] || [ "${_BUILD_MODE}" = "app_4" ]
then 
   echo "BUILD_STAGE APP_4 build started"
fi
if [ "${_BUILD_MODE}" = "all" ] || [ "${_BUILD_MODE}" = "app_5" ]
then 
   echo "BUILD_STAGE APP_5 build started"
fi
if [ "${_BUILD_MODE}" = "all" ] || [ "${_BUILD_MODE}" = "app_6" ]
then 
   echo "BUILD_STAGE APP_6 build started"
fi
2
  • 1
    Add a line showing how you assign _BUILD_MODE. I say that because I just pasted your commands into a script, and they worked perfectly.
    – RonJohn
    Apr 5, 2023 at 7:35
  • So maybe add the entire script for analysis... please don't answer in comments, but edit your question instead to keep it up-to-date and relevant info available. Apr 5, 2023 at 8:01

2 Answers 2

3

That does not happen with the sample code you're giving here. Presumably those echos are just place holders for more complicated tasks you're performing, my guess would be that the task for app_2 actually changes the value of $_BUILD_MODE.

Here, you could do:

case $_BUILD_MODE in
  (all | app_1) task for app_1 ;;&
  (all | app_2) task for app_2 ;;&
  (all | app_3) task for app_3 ;;&
  ...
esac

Which would make it shorter, neater and also avoid that particular issue as $_BUILD_MODE is dereferenced only once at the beginning.

;;& above is bash-specific. In zsh and mksh, the equivalent syntax is with ;|. zsh, mksh and bash support ;; (from the Bourne shell and the only standard one) and ;& (from the Korn shell).

It's also possible that the task for app_2 calls exit or exec some-other-command or runs into a fatal error or has the shell process killed¹. A work around for that would be to perform the tasks in subshells by wrapping them inside (...).

Try running the script with bash -o xtrace (same as bash -x) to see what happens.


¹ for instance by a SIGPIPE if a builtin writes to a broken pipe or by SIGXCPU if it exceeds some resource limit...

1

Not really about what you're asking, but that sort of if-chains look awkward as you're repeating some pieces of code there, even if it's just the [ "${_BUILD_MODE}" = "all" ] here.

Instead, I'd put the tasks into e.g. functions and make an array with their names (untested):

app_1() {
    echo "BUILD_STAGE APP_1 build... "
}
app_2() {
    echo "BUILD_STAGE APP_2 build... "
}
tasks=(app_1 app_2)

for task in "${tasks[@]}"; do 
    if [ "${_BUILD_MODE}" = "all" ] || [ "${_BUILD_MODE}" = "$task" ]; then
        "$task"
    fi
done

Note that the function names must be identical to the task names as used in _BUILD_MODE here.

Here, if you need to do changes to how the tasks run, e.g. by wrapping them in a subshell like Stéphane suggests, or add some printout to separate them, you can do it one place in the loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .