Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple statements in if else statement in shell scripting

Tags:

I want to ask is it possible to give multiple statements to be executed when if condition gets satisfied by doing just

if[condition] then statements else statements fi 

or we have to do something else like using do.....done around that block of statements

like image 512
Shweta Avatar asked Feb 22 '11 07:02

Shweta


People also ask

How do you write multiple if-else statements in shell script?

To use multiple conditions in one if-else block, then elif keyword is used in shell. If expression1 is true then it executes statement 1 and 2, and this process continues. If none of the condition is true then it processes else part.

Can you have multiple if statements in bash?

Bash else-if statement is used for multiple conditions. It is just like an addition to Bash if-else statement. In Bash elif, there can be several elif blocks with a boolean expression for each one of them. In the case of the first 'if statement', if a condition goes false, then the second 'if condition' is checked.

What is in if condition in shell script?

The keyword if is followed by a condition. This condition is evaluated to decide which statement will be executed by the processor. If the condition evaluates to TRUE, the processor will execute the statement(s) followed by the keyword then. In the syntax, it is mentioned as statement1.

What are the 2 conditional statements that can be used in a shell script select two?

Two types of conditional statements can be used in bash. These are, 'If' and 'case' statements. 'If' statements can be used to check the conditions in multiple ways.


1 Answers

Yes you can have multiple statements:

if [ condition ]; then   echo 'foo'   echo 'bar' else   echo 'hello'   echo 'world' fi 
like image 121
codaddict Avatar answered Sep 21 '22 05:09

codaddict