How to execute a second condition if the first condition is true in Linux shell scripting

5,226

Solution 1

How to execute second condition if first condition true

Your code needs to have the following structure:

if [condition -ne 0]
then 
  do_something 
  if [expression that depends on exit status of do_something]
  then 
    do something_else 
  fi 
fi

SYNTAX

if test-commands; then
  consequent-commands;
[elif more-test-commands; then
   more-consequents;]
[else alternate-consequents;]
fi

The test-commands list is executed, and if its return status is zero, the consequent-commands list is executed.

If test-commands returns a non-zero status, each elif list is executed in turn, and if its exit status is zero, the corresponding more-consequents is executed and the command completes.

If else alternate-consequents is present, and the final command in the final if or elif clause has a non-zero exit status, then alternate-consequents is executed.

The return status is the exit status of the last command executed, or zero if no condition tested true.

See below for a link with examples.


Further Reading

Solution 2

It sounds like

first-condition && consequences1

Followed by:

(again)

first-condition && second-condition && consequences2

This may be changed to if;then;fi by inserting a secondary if into the original:

if [ first-condition ]; then
   consequences1
   if [ second-condition ]; then # Because first-condition is already true here
      consequences2
   fi
fi

So your consequences2 is NOT an "else" issue at all :) Nesting is fun.

If your second-condition is simply based on the output of consequences1, you can run that in the test of the second if:

if [ first-condition ]; then
  if [ consequences1-test ]; then
    consequences2
  fi
 fi

It should sweeten your pot. Just recall that ANYTHING can be run in a test; only the returncode of the last command (or the output on stdio), but you'll need to test something then: like

[ "`consequences1`" == "allok" ]

matters

Share:
5,226
bunny
Author by

bunny

Updated on September 18, 2022

Comments

  • bunny
    bunny over 1 year

    I have an if condition. If that condition is true then one script will be run and after that I need to check another condition.

    How can I do that using an if statement or something else?

    For example,

     if [ condition -eq o ]
     then
     run script
     again condition(this condition depends on above script output value)
     run another script
     else
     exit
    
  • bunny
    bunny over 8 years
    Thanks for the suggestion but my point is that , if first condition returns zero status then i need run other condition ,if first condition returns non zero value then no need of executing other commands/conditions.
  • DavidPostill
    DavidPostill over 8 years
    @bunny Use -ne instead of -eq See Conditional Expressions - Conditional expressions are used by bracketed expressions and the test builtin.
  • bunny
    bunny over 8 years
    Any suggesstions from anyone ?
  • DavidPostill
    DavidPostill over 8 years
    @bunny I alread told you what you need to do. Use -ne instead of -eq in the first if expression. Please spend some time reading the links I gave in the answer section "Further reading". We cannot write the code for you as you have only written psuedo-code not real code.
  • DavidPostill
    DavidPostill over 8 years
    @bunny Answer updated. The rest is up to you.
  • Hannu
    Hannu over 8 years
    Suggestion: Avoid 'backticks' and use $(...) instead, for clarity. :-)
  • bunny
    bunny over 8 years
    Below is my script .Please check.