0

I am trying to write a shell script which has a part where I have to use some kind of loop to execute some commands if condition satisfy. I want to understand what is the best way to approach this problem.

Lets say there are 3 flags

#export flag1=1
#export flag2=1
#export flag3=1

These are commented in the beginning. While running the script, I want to enable these flags one after the other.

So for that I have written a if else loop in such way.

if [[ -n ${flag1} ]]; then
    #some commands
elif [[ -n "${flag2}" ]]; then
    #some commands
elif [[ -n ${flag3} ]]; then
    #some commands
else
    #some commands
fi

But, when I enable flag1 it goes to else loop and it's giving different result than expected.

What's the best way to approach this issue?

Should I use case instead of if else loop ?

7
  • 3
    sooo instead of elif do fi; if? giving different result than expected. what exactly is expected? There are 3 flags, which gives 2³ = 8 combinations of flags beeing set and unset. Please specify what exactly is expected for each of combinations of flags. if else loop A "loop" is a repeating part of code, it executes multiple times as in "in a loop". The expressions inside if execute once. They are usually called a "body", like "if body" or "else body", and they both execute once.
    – KamilCuk
    Commented Jun 5 at 6:29
  • my doubt is when I uncomment flag1 the else block in the loop is executed not the first if part . why is that ?
    – abssyz
    Commented Jun 5 at 6:33
  • 1
    So just test it? Add echo 1 2 3 4 to each body and test it. I think the best way to deal with your "doubt" is to proof what actually happens.
    – KamilCuk
    Commented Jun 5 at 6:35
  • 1
    An if … else block is not a loop, i.e. code is not executed repeatedly, just once and on condition. For a loop (that may repeat executing a block of code) use for or while. If needed, both structures can also be nested inside each other. As for your core issue, however, you only seem to need conditional blocks, i.e. if or case. It looks like you want to test the flags independently, so either use one if per condition (don't branch to else), or use a single case block with fall-throughs (;& or ;;& instead of ;;).
    – pmf
    Commented Jun 5 at 7:03
  • 1
    can't reproduce your issue, Just copy your script and it works correctly
    – Saboteur
    Commented Jun 5 at 11:36

1 Answer 1

3

You are doing it backward. The logic and algorithm goes before the code.

So you have 3 flags. If these are binary, then draw a boolean truth table with all 3 binary flags and a result column. With 3 flags, you have 2³ states, so 8-states to deal with. Then find what boolean algebra / logic applies between those flags and factorize into an if/else with a branch for each different outcome and factored boolean logic as conditions.

Now if these are not 3 binary flags with 8-states, then you may mean a 3-states value. States are strictly distinct. In this later scenario, a switch (shell case) statement is the right tool.

To sum it up. The analysis of the problem to solve determines what code to use. Laying code prematurely leads to logic errors and inappropriate algorithms.

If you have done the analysis right. The code to use is straightforward.

Not the answer you're looking for? Browse other questions tagged or ask your own question.