Decision Making

A Shell script is a plain text file. This file contains different commands for step-by-step execution.

These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file.

Two types of decision-making statements are used within shell scripting. They are –

  • If-else statement

  • case-sac statement

If-else

If else statement is a conditional statement. It can be used to execute two different codes based on whether the given condition is satisfied or not.

There are a couple of varieties present within the if-else statement.

  • if-fi

  • if-else-fi

  • if-elif-else-fi

  • nested if-else

if-fi

if  [[ some_test ]] 

then 	
	commands
fi

Example:

if-else-fi

Example:

if-elif-else

Example:

Switch-case

you can use a case statement to simplify complex conditionals when there are multiple different choices.

So rather than using a few if, and if-else statements, you could use a single case statement.

Example

Last updated