LWM-Linux/07 - Shell Scripting Basics/Conditional Statements.md

231 lines
3.6 KiB
Markdown
Raw Normal View History

# Conditional Statements in Bash
Conditional statements in Bash allow you to control the flow of your script based on certain conditions. They enable your script to make decisions and execute different code blocks depending on whether specific conditions are true or false.
## 1. if statement
The most basic conditional statement is the 'if' statement. Its syntax is:
2024-09-07 10:14:32 -06:00
```
if [ condition ]; then
2024-09-07 10:14:32 -06:00
# commands to execute if condition is true
fi
```
Example:
2024-09-07 10:14:32 -06:00
```
#!/bin/bash
age=18
if [ $age -ge 18 ]; then
2024-09-07 10:14:32 -06:00
echo "You are an adult."
fi
```
## 2. if-else statement
The if-else statement allows you to specify actions for both when the condition is true and when it's false:
2024-09-07 10:14:32 -06:00
```
if [ condition ]; then
2024-09-07 10:14:32 -06:00
# commands to execute if condition is true
else
2024-09-07 10:14:32 -06:00
# commands to execute if condition is false
fi
```
Example:
2024-09-07 10:14:32 -06:00
```
#!/bin/bash
age=16
if [ $age -ge 18 ]; then
2024-09-07 10:14:32 -06:00
echo "You are an adult."
else
2024-09-07 10:14:32 -06:00
echo "You are a minor."
fi
```
## 3. if-elif-else statement
For multiple conditions, use the if-elif-else structure:
2024-09-07 10:14:32 -06:00
```
if [ condition1 ]; then
2024-09-07 10:14:32 -06:00
# commands for condition1
elif [ condition2 ]; then
2024-09-07 10:14:32 -06:00
# commands for condition2
elif [ condition3 ]; then
2024-09-07 10:14:32 -06:00
# commands for condition3
else
2024-09-07 10:14:32 -06:00
# commands if none of the conditions are true
fi
```
Example:
2024-09-07 10:14:32 -06:00
```
#!/bin/bash
grade=75
if [ $grade -ge 90 ]; then
2024-09-07 10:14:32 -06:00
echo "A"
elif [ $grade -ge 80 ]; then
2024-09-07 10:14:32 -06:00
echo "B"
elif [ $grade -ge 70 ]; then
2024-09-07 10:14:32 -06:00
echo "C"
elif [ $grade -ge 60 ]; then
2024-09-07 10:14:32 -06:00
echo "D"
else
2024-09-07 10:14:32 -06:00
echo "F"
fi
```
## 4. Comparison operators
Bash uses different operators for string and numeric comparisons:
Numeric comparisons:
- -eq: equal to
- -ne: not equal to
- -lt: less than
- -le: less than or equal to
- -gt: greater than
- -ge: greater than or equal to
String comparisons:
- =: equal to
- !=: not equal to
- <: less than (in ASCII alphabetical order)
2024-09-07 10:14:32 -06:00
- \>: greater than (in ASCII alphabetical order)
- -z: string is null (zero length)
- -n: string is not null
Example:
2024-09-07 10:14:32 -06:00
```
#!/bin/bash
num1=10
num2=20
str1="hello"
str2="world"
if [ $num1 -lt $num2 ]; then
2024-09-07 10:14:32 -06:00
echo "$num1 is less than $num2"
fi
if [ $str1 != $str2 ]; then
2024-09-07 10:14:32 -06:00
echo "$str1 is not equal to $str2"
fi
```
## 5. Logical operators
Bash supports logical AND and OR operations:
2024-09-07 10:14:32 -06:00
- &&: AND # Double ampersand Shift+7 (qwerty)
- ||: OR # Double Pipe | | with no spaces!
Example:
2024-09-07 10:14:32 -06:00
```
#!/bin/bash
age=25
has_license=true
if [ $age -ge 18 ] && [ "$has_license" = true ]; then
2024-09-07 10:14:32 -06:00
echo "You can drive a car."
fi
if [ $age -lt 18 ] || [ "$has_license" != true ]; then
2024-09-07 10:14:32 -06:00
echo "You cannot drive a car."
fi
```
## 6. Case statement
The case statement is useful when you have multiple conditions based on a single variable:
2024-09-07 10:14:32 -06:00
```
case $variable in
2024-09-07 10:14:32 -06:00
pattern1)
# commands for pattern1
;;
pattern2)
# commands for pattern2
;;
*) # This is the default or catch-all case
# default case
;;
esac
```
Example:
2024-09-07 10:14:32 -06:00
```
#!/bin/bash
fruit="apple"
case $fruit in
2024-09-07 10:14:32 -06:00
"apple")
echo "This is a red fruit."
;;
"banana")
echo "This is a yellow fruit."
;;
"grape")
echo "This is a purple fruit."
;;
*)
echo "Unknown fruit."
;;
esac
```
## 7. Test command
The test command is often used in conditional statements. It's equivalent to using square brackets []. You can use it like this:
2024-09-07 10:14:32 -06:00
```
if test $a -eq $b; then
2024-09-07 10:14:32 -06:00
echo "a is equal to b"
fi
```
This is the same as:
2024-09-07 10:14:32 -06:00
```
if [ $a -eq $b ]; then
echo "a is equal to b"
fi
```
## 8. Double square brackets
Bash also supports double square brackets [[ ]] for conditional tests. These provide more features than single brackets, such as pattern matching:
2024-09-07 10:14:32 -06:00
```
#!/bin/bash
string="Hello, World!"
if [[ $string == Hello* ]]; then
2024-09-07 10:14:32 -06:00
echo "String starts with 'Hello'"
fi
```
Double brackets also allow you to use && and || inside the condition:
2024-09-07 10:14:32 -06:00
```
if [[ $a -eq 5 && $b -gt 10 ]]; then
2024-09-07 10:14:32 -06:00
echo "Condition met"
fi
2024-09-07 10:14:32 -06:00
```