# Decision Making

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

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&#x20;
* if-else-fi&#x20;
* if-elif-else-fi&#x20;
* nested if-else

#### if-fi

<pre class="language-bash"><code class="lang-bash">if  [[ some_test ]] 

then 	
<strong>	commands
</strong>fi
</code></pre>

Example:

```bash
#!/bin/bash 

# Bash if statement example 

read -p "What is your name? " name 

if [[ -z ${name} ]] 

then 
echo "Please enter your name!" 

fi 
```

#### if-else-fi

```bash
if  [ expression ] 

then 	
	Commands
else
	Commands
fi
```

Example:

```bash
#!/bin/bash 

# Bash if statement example 

read -p "What is your name? " name 

if [[ -z ${name} ]] 

then 

echo "Please enter your name!" 
else 
echo "Hi there ${name}" 

fi
```

#### if-elif-else

```bash
if  [ expression ] 

then 
	Commands
elif [ expression ] 

	commands
else
	Commands
fi

```

Example:

```bash
#!/bin/bash 

read -p "Enter a number: " num 

if [[ $num -gt 0 ]] ; then 

echo "The number is positive" 

elif [[ $num -lt 0 ]] ; then 

echo "The number is negative" 

else 
echo "The number is 0" 

fi
```

### Switch-case

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

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

```bash
case $some_variable in 

pattern_1) 

commands 

;; 

pattern_2| pattern_3) 

commands 

;; 

*) 

default commands 

;; 
esac

```

Example

```bash
#!/bin/bash 

read -p "Enter the name of your car brand: " car 

case $car in 
Tesla) 
echo -n "${car}'s car factory is in the USA." 
;; 
BMW | Mercedes | Audi | Porsche) 
echo -n "${car}'s car factory is in Germany." 
;; 
Toyota | Mazda | Mitsubishi | Subaru) 
echo -n "${car}'s car factory is in Japan." 
;; 
*) 
echo -n "${car} is an unknown car brand" 
;; 

esac 

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devops-3.gitbook.io/devops/docs/module-3-bash-scripting/decision-making.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
