# `if` statements
- `if` statements check to see if a `test` condition is true
- if true, perform then segment
- if not true, perform else statement
- ends with `fi`
- simple structure using brackets in place of `test`
```bash
#!/bin/bash
if [ condition ]; then
# commands to execute if condition true
else
# commands to execute if condition untrue
fi
```
- example `if` statement with command line input and integer comparison
```bash
#!/bin/bash
if [ $1 -ne 10 ]; then
echo "input is not 10"
else
echo "input is 10"
fi
```
- example `if` statement using command substitution and string comparison
```bash
#!/bin/bash
if [ $(whoami) == "root" ]; then
echo "You're root! GZ"
else
echo "You're a puny, normal user."
fi
```
![[images/Pasted image 20250808150628.png]]
- single brackets are a substitute for `test`
- double brackets provide for extended `test` and additional flexibility; BASH only - not POSIX compliant
- `==` and `!=` work with strings
- `-lt`, `-gt`, `-le`, `-ge`, `-eg`, `-ne` work with integers
- simple example of double brackets with string comparison that leverages user input
- `read` pulls user input into script and assigns to a variable
```bash
#!/bin/bash
echo "please input a string"
read input
if [[ $input == "test" ]]; then
echo "You entered test. That's what I was looking for."
else
echo "Keep trying. This might take a while."
fi
```
![[images/Pasted image 20250808145321.png]]
- add double pipes for disjunctive string comparisons tests
```bash
#!/bin/bash
echo "please input a string"
read input
if [[ $input == "test" || $input == "hello ]]; then
echo "You entered test/hello. That's what I was looking for."
else
echo "Keep trying. This might take a while."
fi
```
![[images/Pasted image 20250808145514.png]]
## Using regex for multi-part conditionals
- alternative multi-part test with regular expressions using `@(x|y|z)`
- double brackets necessary when using regex
```bash
#!/bin/bash
echo "please input a string"
read input
if [[ $input == @(yes|test|hello) ]]; then
echo "You entered yes/test/hello. That's what I was looking for."
else
echo "Keep trying. This might take a while."
fi
```
![[images/Pasted image 20250808145649.png]]
- double condition `if` statement
- the example script below asks for two user inputs and provides output when two conditions are true with `&&` separating the two tests
```bash
#!/bin/bash
echo "Please input first name"
read first
echo "Please input last name"
read last
if [[ ($first == @(jake|jacob) || $first == "jim") && $last == "smith" ]]; then
echo "You're one of the smith bros!"
else
echo "You're different."
fi
```
![[images/Pasted image 20250808152303.png]]
# `elif` statements
- `elif` statements provide for different results/outputs when alternative conditions are true
```bash
#!/bin/bash
echo "please input a string"
read input
if [[ $input == "test" ]]; then
echo "You entered test. That's what I was looking for."
elif [[ $input == "hello" ]]; then
echo "You entered hellow. Nice"
else
echo "Keep trying. This might take a while."
fi
```
![[images/Pasted image 20250808145928.png]]
- double condition `if` statement
- the example script below asks for two user inputs and provides output when two conditions are true with `&&` separating the two tests
```bash
#!/bin/bash
echo "Please input first name"
read first
echo "Please input last name"
read last
if [[ ($first == @(jake|jacob) || $first == "jim") && $last == "smith" ]]; then
echo "You're one of the smith bros!"
else
echo "You're different."
fi
```
![[images/Pasted image 20250808152303.png]]
# `case` statements
- `case` statements provide for multi-part tests each with a different output
- end each test/section with `;;`
- `*` is a catchall almost like `else`
- end statement with `esac`, which is case spelled backward
- multi-part string comparison `case` example
```bash
#!/bin/bash
echo "please input a string"
read input
case $input in
yes)
echo "You input yes...";;
test)
echo "You input test. Smart!";;
abc)
echo "I hope you don't use that for your password :)";;
*)
echo "You input random stuff.";;
esac
```
![[images/Pasted image 20250808151833.png]]
# File tests
- common string/file tests![[images/Pasted image 20250804194347.png]]
- Example script tests for existence of file/folder/link named `test.txt` within current directory that is *not* empty with `-s` test
```bash
#!/bin/bash
if [[ -s test.txt ]]; then
echo "Test.txt exists in current directory and is not empty"
else
echo "Either test.txt does not exist in current directory or it's empty."
fi
echo "Current directory = `pwd`"
```
![[images/Pasted image 20250808160700.png]]
- test for existence of file/folder/link regardless of size) with `-a` or `-e` test
- test for existence of file (regardless of size) with `-f` test
- test for existence of folder (regardless of size) with `-d` test
# Integer Tests
- must use integers when dealing with conditional BASH statements
- BASH is not the best for dealing with floating point numbers
- can call into BASH with other programs that are better at handling floats like `bc`
- otherwise need error handling such as rounding/replacement built into script
- common arithmetic tests ![[images/Pasted image 20250804194327.png]]
- can also use symbols such as >, >=, <, <=, `==`, `!=`
- example script using `-eq` to compare whether user input integers are equal
```bash
#!/bin/bash
echo "Provide a first interger"
read first
echo "Good. Provide a second integer, please."
read second
if [[ $first -eq $second ]]; then
echo "You gave me the same integer numbers!"
else
echo "Nice. You provided two different integer numbers."
fi
```
![[images/Pasted image 20250808161524.png]]
- example of simple math within a conditional using double parentheses `$(( math ))`
```bash
#!/bin/bash
echo "Provide a first interger"
read first
echo "Good. Provide a second integer, please."
read second
if [[ $(( $first * 2 )) -eq $second ]]; then
echo "The first int was half of the second!"
else
echo "Nice. You provided two different integer numbers."
fi
```
![[images/Pasted image 20250808161722.png]]
- example of conditional using short notation at command line
- `[[1 -eq 1]] && echo "true" || echo "false)`
- if then else statement in short