How to execute a second condition if the first condition is true in Linux shell scripting
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, theconsequent-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 correspondingmore-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, thenalternate-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
- An A-Z Index of the Bash command line for Linux - An excellent reference for all things Bash command line related.
- Conditional Expressions - Conditional expressions are used by bracketed expressions and the test builtin.
- if - Conditionally perform a command.
- Bash If Statement Examples (If then fi, If then else fi, If elif else fi, Nested if)
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
bunny
Updated on September 18, 2022Comments
-
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 over 8 yearsThanks 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 over 8 years@bunny Use
-ne
instead of-eq
See Conditional Expressions - Conditional expressions are used by bracketed expressions and the test builtin. -
bunny over 8 yearsAny suggesstions from anyone ?
-
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 over 8 years@bunny Answer updated. The rest is up to you.
-
Hannu over 8 yearsSuggestion: Avoid 'backticks' and use
$(...)
instead, for clarity. :-) -
bunny over 8 yearsBelow is my script .Please check.