Nested if loop

Hi Team,

I just want to check whether my nested if loop used is correct or not. 
if [[ condition1]] 
  if [[condition2]]
     export1
   else
     export2
  fi
else
  if [[condition2]]
    export3
   else
     export4
  fi
fi

Thanks
Shiva

Expecting this ..??

if [ condition1 ]; then
   export 1
elif [ conidtion2 ]; then
   export 2
elif [ condition3 ]; then
   export 3
else
   export 4
fi
1 Like

It is not a loop. The syntax is correct, except for the left-out then statements. It is a good idea to use proper indenting:

if [[ condition1]]; then
  if [[condition2]]; then
    export1
  else
    export2
  fi
else
  if [[condition2]]; then
    export3
  else
    export4
  fi
fi

The [[ .. ]] is bash/ks93 syntax. A more universal syntax is [ .. ] , but then you should make sure there is a space around the brackets:

if [ condition1 ]; then
  if [ condition2 ]; then
    export1
  else
    export2
  fi
else
  if [ condition2 ]; then
    export3
  else
    export4
  fi
fi

My code has a if loop both in main if part and also in else part.

---------- Post updated at 04:25 AM ---------- Previous update was at 04:20 AM ----------

I used "then" in my code, but forgot to use it here.

if is a conditional statement not looping !

while, for are loop statements...

1 Like
if [ condition 1 ]; then
  if [ condition 2 ]; then
     export 1
  else
     export 2
   fi
elif [ ~condition 1 ]; then
   if [ condition 2 ]; then
      export 3
   else 
      export 4
    fi
fi

Is my nested if statement is correct.
here "export" commands has 4 different values; so i numbered it like that.
"~ condition 1" - inverse of the condition 1

Thanks
Shiva