Linux CLI 59 ๐ง shell scripts flow control and if
59 – shell functions flow control and if
#!/bin/bash
# Get a number from user input
read -p "Enter a number: " num
if [ $num -lt 0 ]; then
echo "The number is negative."
elif [ $num -eq 0 ]; then
echo "The number is zero."
else
echo "The number is positive."
fi
num1=10
num2=5
sum=$((num1 + num2))
difference=$((num1 - num2))
product=$((num1 * num2))
quotient=$((num1 / num2))
remainder=$((num1 % num2))
echo "Sum: $sum"
echo "Difference: $difference"
echo "Product: $product"
echo "Quotient: $quotient"
echo "Remainder: $remainder"
num1=10
num2=5
if [ $num1 -gt 0 ] && [ $num2 -gt 0 ]; then
echo "Both numbers are positive."
else
echo "At least one number is not positive."
fi
string1=""
if [ -z "$string1" ]; then
echo "String is empty."
else
echo "String is not empty."
fi
Flow control is what turns a script from a straight line of commands into a program that makes decisions. The if statement is the most important of these โ it lets you run different code depending on whether a condition is true or false. Combined with operators for comparing numbers, strings, and logic, you can build scripts that respond to their environment.
Key point: In shell scripting, if tests the exit status of a command โ 0 means true, anything else means false. The [ ... ] (or [[ ... ]]) syntax is actually a command that returns 0 or 1 based on the condition inside.
a – flow control in shell scripts
Flow control is an essential aspect that allows you to manage the execution of commands based on certain conditions or loops. It helps in structuring the script to perform different tasks under varying circumstances.
Shell scripting supports several flow control constructs:
| Construct | Purpose |
|---|---|
if | Run code if a condition is true |
if / else | Two-way branch |
if / elif / else | Multi-way branch |
case | Match against multiple patterns |
for | Loop over a list |
while | Loop while a condition is true |
until | Loop until a condition is true |
break | Exit a loop early |
continue | Skip to the next iteration |
Key flow control constructs:
if,if-else, andelif(else-if) โ decide which block to run- Loops โ
for,while, anduntilโ repeat a block
This chapter focuses on if and its variants. Loops come next.
Why flow control matters:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Without flow control โ
โ โ
โ cmd1 โ
โ cmd2 โ
โ cmd3 โ
โ cmd4 โ
โ โ
โ Always runs in the same order. โ
โ Can't respond to anything. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ With flow control โ
โ โ
โ cmd1 โ
โ if condition; then โ
โ cmd2 โ only if true โ
โ else โ
โ cmd3 โ only if false โ
โ fi โ
โ cmd4 โ
โ โ
โ Decides what to do based on the situation. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
b – if, else and elif in shell scripts
if statements are fundamental components for controlling the flow of execution in shell scripts. They allow parts of your script to run only if certain conditions are met, making your scripts more dynamic and responsive.
Basic syntax:
if [ condition ]; then
# Commands to run if condition is true
elif [ condition ]; then
# Commands to run if the first was false and this is true
else
# Commands to run if all conditions are false
fi
How if works:
if runs a command and checks its exit status:
0โ true โ run thethenblock- non-zero โ false โ check
elif, thenelse
Example โ classify a number:
#!/bin/bash
read -p "Enter a number: " num
if [ $num -lt 0 ]; then
echo "The number is negative."
elif [ $num -eq 0 ]; then
echo "The number is zero."
else
echo "The number is positive."
fi
| Input | Output |
|---|---|
-5 | The number is negative. |
0 | The number is zero. |
42 | The number is positive. |
Breaking it down:
read -p "Enter a number: " numโ prompts the user to enter a numberif [ $num -lt 0 ]; thenโ checks ifnumis less than 0elif [ $num -eq 0 ]; thenโ checks ifnumis equal to 0elseโ if all previous conditions were false, run this block
Simple if (no else):
if [ -f /etc/passwd ]; then
echo "File exists"
fi
if / else (two branches):
if [ -f /etc/passwd ]; then
echo "File exists"
else
echo "File not found"
fi
if / elif / else (multiple branches):
if [ $grade -ge 90 ]; then
echo "A"
elif [ $grade -ge 80 ]; then
echo "B"
elif [ $grade -ge 70 ]; then
echo "C"
elif [ $grade -ge 60 ]; then
echo "D"
else
echo "F"
fi
Nested if:
if [ -f "$file" ]; then
if [ -r "$file" ]; then
echo "File exists and is readable"
else
echo "File exists but is not readable"
fi
else
echo "File not found"
fi
[ ] vs [[ ]]:
| Feature | [ ] (POSIX) | [[ ]] (bash) |
|---|---|---|
| Word splitting | Yes | No |
| Globbing | Yes | No |
&& / || inside | No | Yes |
Regex =~ | No | Yes |
| Pattern matching | No | Yes |
| Portability | All shells | Bash, ksh, zsh |
# [ ] requires quoting
if [ "$name" = "Alice" ]; then ...; fi
# [[ ]] doesn't
if [[ $name == "Alice" ]]; then ...; fi
# [[ ]] supports &&
if [[ $a -gt 0 && $b -gt 0 ]]; then ...; fi
# [[ ]] supports regex
if [[ $email =~ @.*\.com$ ]]; then ...; fi
# [[ ]] supports glob patterns
if [[ $file == *.txt ]]; then ...; fi
Use [[ ]] when:
- You’re writing bash-specific scripts
- You want to avoid quoting
- You need
&&/||inside the test - You need regex or glob matching
Use [ ] when:
- You need POSIX portability
- You’re writing for
shordash
The test command:
[ ... ] is actually a command โ test. These are equivalent:
if [ -f file ]; then ...; fi
if test -f file; then ...; fi
Common file test operators:
| Operator | True if |
|---|---|
-e FILE | File exists |
-f FILE | Regular file |
-d FILE | Directory |
-r FILE | Readable |
-w FILE | Writable |
-x FILE | Executable |
-s FILE | Non-empty |
-L FILE | Symlink |
if [ -d /home/kronos ]; then
echo "Directory exists"
fi
if [ -x /usr/bin/ls ]; then
echo "ls is executable"
fi
case โ the multi-way alternative:
When you have many branches based on the same value, case is cleaner than a chain of elif:
case "$1" in
start) echo "Starting..." ;;
stop) echo "Stopping..." ;;
restart) echo "Restarting..." ;;
*) echo "Usage: $0 {start|stop|restart}" ;;
esac
c – operators in shell scripts
Operators in shell scripts are essential for performing comparisons, logical operations, and arithmetic calculations.
Types of operators:
- Arithmetic operators
- Comparison (relational) operators
- Logical operators
- String operators
Arithmetic operators:
| Operator | Meaning |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo (remainder) |
** | Exponentiation (bash) |
Arithmetic in $((...)):
num1=10
num2=5
sum=$((num1 + num2))
difference=$((num1 - num2))
product=$((num1 * num2))
quotient=$((num1 / num2))
remainder=$((num1 % num2))
echo "Sum: $sum"
echo "Difference: $difference"
echo "Product: $product"
echo "Quotient: $quotient"
echo "Remainder: $remainder"
[ Sum: 15 ]
[ Difference: 5 ]
[ Product: 50 ]
[ Quotient: 2 ]
[ Remainder: 0 ]
Note: Bash arithmetic is integer only.
10 / 3gives3, not3.333. For floating point, usebcorawk.
$ echo "scale=2; 10 / 3" | bc
[ 3.33 ]
$ awk 'BEGIN {print 10 / 3}'
[ 3.33333 ]
Increment and decrement:
$ i=5
$ ((i++))
$ echo $i
[ 6 ]
$ ((i--))
$ echo $i
[ 5 ]
$ ((i += 10))
$ echo $i
[ 15 ]
Comparison (relational) operators:
For numbers, use the alphabetic forms:
| Operator | Meaning |
|---|---|
-eq | Equal to |
-ne | Not equal to |
-gt | Greater than |
-ge | Greater than or equal to |
-lt | Less than |
-le | Less than or equal to |
if [ $num -gt 0 ]; then
echo "Positive"
fi
if [ $a -eq $b ]; then
echo "Equal"
fi
if [ $age -ge 18 ]; then
echo "Adult"
fi
For strings, use symbols:
| Operator | Meaning |
|---|---|
= | Equal to |
!= | Not equal to |
< | Less than (ASCII) |
> | Greater than (ASCII) |
if [ "$name" = "Alice" ]; then
echo "Hello, Alice"
fi
if [ "$a" != "$b" ]; then
echo "Different"
fi
โ ๏ธ Warning:
>and<inside[ ]need escaping (\>) or use[[ ]]โ otherwise the shell treats them as redirection!
# โ Wrong โ creates a file called "file"
if [ "$a" > "$b" ]; then ...; fi
# โ
Correct
if [ "$a" \> "$b" ]; then ...; fi
if [[ "$a" > "$b" ]]; then ...; fi
Logical operators:
| Operator | Meaning |
|---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
num1=10
num2=5
if [ $num1 -gt 0 ] && [ $num2 -gt 0 ]; then
echo "Both numbers are positive."
else
echo "At least one number is not positive."
fi
if [ $age -lt 18 ] || [ $age -gt 65 ]; then
echo "Discounted ticket"
fi
if ! [ -f "$file" ]; then
echo "File not found"
fi
With [[ ]], combine inside:
if [[ $a -gt 0 && $b -gt 0 ]]; then
echo "Both positive"
fi
if [[ $name == "Alice" || $name == "Bob" ]]; then
echo "Known user"
fi
String operators:
| Operator | Meaning |
|---|---|
= | Equal to |
!= | Not equal to |
-z | Null string (length zero) |
-n | Non-null string |
< | Less than in ASCII order |
> | Greater than in ASCII order |
string1=""
if [ -z "$string1" ]; then
echo "String is empty."
else
echo "String is not empty."
fi
[ String is empty. ]
string2="hello"
if [ -n "$string2" ]; then
echo "String has content."
fi
[ String has content. ]
Always quote strings in [ ]:
# โ Wrong โ if $name is empty, this becomes [ = "Alice" ] โ error
if [ $name = "Alice" ]; then ...; fi
# โ
Correct
if [ "$name" = "Alice" ]; then ...; fi
Operator summary table:
| Category | Operator | Use |
|---|---|---|
| Arithmetic | + - * / % | Inside $((...)) |
| Numeric compare | -eq -ne -gt -ge -lt -le | Inside [ ] |
| String compare | = != < > | Inside [ ] or [[ ]] |
| String test | -z -n | Inside [ ] |
| File test | -e -f -d -r -w -x -s -L | Inside [ ] |
| Logical | && || ! | Between [ ] blocks or inside [[ ]] |
A complete example โ classify a number and a string:
#!/bin/bash
read -p "Enter a number: " num
if [ "$num" -lt 0 ]; then
echo "The number is negative."
elif [ "$num" -eq 0 ]; then
echo "The number is zero."
else
echo "The number is positive."
fi
num1=10
num2=5
sum=$((num1 + num2))
difference=$((num1 - num2))
product=$((num1 * num2))
quotient=$((num1 / num2))
remainder=$((num1 % num2))
echo "Sum: $sum"
echo "Difference: $difference"
echo "Product: $product"
echo "Quotient: $quotient"
echo "Remainder: $remainder"
if [ $num1 -gt 0 ] && [ $num2 -gt 0 ]; then
echo "Both numbers are positive."
else
echo "At least one number is not positive."
fi
string1=""
if [ -z "$string1" ]; then
echo "String is empty."
else
echo "String is not empty."
fi
$ ./script.sh
Enter a number: -3
[ The number is negative. ]
[ Sum: 15 ]
[ Difference: 5 ]
[ Product: 50 ]
[ Quotient: 2 ]
[ Remainder: 0 ]
[ Both numbers are positive. ]
[ String is empty. ]
Complete Example Session
# ============================================
# PART 1: BASIC IF
# ============================================
$ num=5
$ if [ $num -gt 0 ]; then
> echo "Positive"
> fi
[ Positive ]
# ============================================
# PART 2: IF / ELSE
# ============================================
$ num=-3
$ if [ $num -gt 0 ]; then
> echo "Positive"
> else
> echo "Not positive"
> fi
[ Not positive ]
# ============================================
# PART 3: IF / ELIF / ELSE
# ============================================
$ num=0
$ if [ $num -lt 0 ]; then
> echo "Negative"
> elif [ $num -eq 0 ]; then
> echo "Zero"
> else
> echo "Positive"
> fi
[ Zero ]
# ============================================
# PART 4: ARITHMETIC
# ============================================
$ num1=10
$ num2=5
$ echo $((num1 + num2))
[ 15 ]
$ echo $((num1 - num2))
[ 5 ]
$ echo $((num1 * num2))
[ 50 ]
$ echo $((num1 / num2))
[ 2 ]
$ echo $((num1 % num2))
[ 0 ]
# ============================================
# PART 5: INCREMENT / DECREMENT
# ============================================
$ i=5
$ ((i++))
$ echo $i
[ 6 ]
$ ((i--))
$ echo $i
[ 5 ]
$ ((i += 10))
$ echo $i
[ 15 ]
# ============================================
# PART 6: LOGICAL OPERATORS
# ============================================
$ a=10
$ b=5
$ if [ $a -gt 0 ] && [ $b -gt 0 ]; then
> echo "Both positive"
> fi
[ Both positive ]
$ if [ $a -gt 0 ] || [ $b -gt 100 ]; then
> echo "At least one true"
> fi
[ At least one true ]
$ if ! [ -f /nonexistent ]; then
> echo "File not found"
> fi
[ File not found ]
# ============================================
# PART 7: STRING COMPARISON
# ============================================
$ name="Alice"
$ if [ "$name" = "Alice" ]; then
> echo "Hello, Alice"
> fi
[ Hello, Alice ]
$ if [ "$name" != "Bob" ]; then
> echo "Not Bob"
> fi
[ Not Bob ]
# ============================================
# PART 8: STRING EMPTY / NON-EMPTY
# ============================================
$ s=""
$ if [ -z "$s" ]; then
> echo "Empty"
> fi
[ Empty ]
$ s="hello"
$ if [ -n "$s" ]; then
> echo "Not empty"
> fi
[ Not empty ]
# ============================================
# PART 9: FILE TESTS
# ============================================
$ if [ -f /etc/passwd ]; then
> echo "File exists"
> fi
[ File exists ]
$ if [ -d /home ]; then
> echo "Directory exists"
> fi
[ Directory exists ]
$ if [ -x /usr/bin/ls ]; then
> echo "Executable"
> fi
[ Executable ]
# ============================================
# PART 10: NESTED IF
# ============================================
$ file=/etc/passwd
$ if [ -f "$file" ]; then
> if [ -r "$file" ]; then
> echo "Readable"
> fi
> fi
[ Readable ]
# ============================================
# PART 11: [[ ]] INSTEAD OF [ ]
# ============================================
$ name="Alice"
$ if [[ $name == "Alice" ]]; then
> echo "Match"
> fi
[ Match ]
$ if [[ $name == A* ]]; then
> echo "Starts with A"
> fi
[ Starts with A ]
$ email="user@example.com"
$ if [[ $email =~ @.*\.com$ ]]; then
> echo "Valid email"
> fi
[ Valid email ]
# ============================================
# PART 12: COMBINING CONDITIONS IN [[ ]]
# ============================================
$ a=10
$ b=5
$ if [[ $a -gt 0 && $b -gt 0 ]]; then
> echo "Both positive"
> fi
[ Both positive ]
# ============================================
# PART 13: CASE STATEMENT
# ============================================
$ action="start"
$ case "$action" in
> start) echo "Starting..." ;;
> stop) echo "Stopping..." ;;
> restart) echo "Restarting..." ;;
> *) echo "Unknown" ;;
> esac
[ Starting... ]
# ============================================
# PART 14: CLASSIFY A NUMBER
# ============================================
$ cat > classify.sh << 'EOF'
#!/bin/bash
read -p "Enter a number: " num
if [ "$num" -lt 0 ]; then
echo "Negative"
elif [ "$num" -eq 0 ]; then
echo "Zero"
else
echo "Positive"
fi
EOF
$ chmod +x classify.sh
$ ./classify.sh
Enter a number: -3
[ Negative ]
$ ./classify.sh
Enter a number: 0
[ Zero ]
$ ./classify.sh
Enter a number: 42
[ Positive ]
# ============================================
# PART 15: GRADE CALCULATOR
# ============================================
$ cat > grade.sh << 'EOF'
#!/bin/bash
read -p "Enter grade: " g
if [ "$g" -ge 90 ]; then
echo "A"
elif [ "$g" -ge 80 ]; then
echo "B"
elif [ "$g" -ge 70 ]; then
echo "C"
elif [ "$g" -ge 60 ]; then
echo "D"
else
echo "F"
fi
EOF
$ chmod +x grade.sh
$ ./grade.sh
Enter grade: 85
[ B ]
Quick Reference
if Syntax
| Form | Example |
|---|---|
| Simple | if [ cond ]; then ...; fi |
| If/else | if [ cond ]; then ...; else ...; fi |
| If/elif/else | if [ c1 ]; then ...; elif [ c2 ]; then ...; else ...; fi |
| One-liner | if [ cond ]; then cmd; fi |
| Negated | if ! cmd; then ...; fi |
Arithmetic Operators
| Operator | Meaning |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo |
** | Exponentiation |
Numeric Comparison
| Operator | Meaning |
|---|---|
-eq | Equal |
-ne | Not equal |
-gt | Greater than |
-ge | Greater or equal |
-lt | Less than |
-le | Less or equal |
String Comparison
| Operator | Meaning |
|---|---|
= | Equal |
!= | Not equal |
-z | Empty |
-n | Non-empty |
< | Less (ASCII) |
> | Greater (ASCII) |
File Tests
| Operator | True if |
|---|---|
-e | Exists |
-f | Regular file |
-d | Directory |
-r | Readable |
-w | Writable |
-x | Executable |
-s | Non-empty |
-L | Symlink |
Logical Operators
| Operator | Meaning |
|---|---|
&& | AND |
|| | OR |
! | NOT |
[ ] vs [[ ]]
| Feature | [ ] | [[ ]] |
|---|---|---|
| Portability | POSIX | Bash |
| Quote needed | Yes | No |
&&/|| inside | No | Yes |
Regex =~ | No | Yes |
| Glob match | No | Yes |
case Syntax
| Pattern | Meaning |
|---|---|
value) | Exact match |
v1|v2) | Multiple values |
pat*) | Glob pattern |
*) | Default |
Best Practices
โ Do This:
# Quote variables in [ ]
if [ "$name" = "Alice" ]; then ...; fi # โ
# Use [[ ]] in bash
if [[ $name == "Alice" ]]; then ...; fi # โ
# Use numeric operators for numbers
if [ "$a" -gt "$b" ]; then ...; fi # โ
# Test in order โ most specific first
if [ "$x" -gt 100 ]; then ... # โ
elif [ "$x" -gt 10 ]; then ...
else ...
# Use case for multiple exact matches
case "$1" in
start) ... ;;
stop) ... ;;
esac # โ
# Use ! for negation
if ! command; then echo "failed"; fi # โ
# Combine with &&
if [ -f "$f" ] && [ -r "$f" ]; then ...; fi # โ
โ Don’t Do This:
# Don't use = for numbers
if [ "$a" = 5 ]; then ...; fi # โ ๏ธ string compare
# Don't use -eq for strings
if [ "$name" -eq "Alice" ]; then ...; fi # โ error
# Don't forget quotes in [ ]
if [ $name = "Alice" ]; then ...; fi # โ breaks if empty
# Don't use > without escaping
if [ "$a" > "$b" ]; then ...; fi # โ creates file "b"
# Don't use elif after else
if ...; then ...
else ...
elif ...; then ... # โ unreachable
fi
# Don't forget fi
if [ -f file ]; then
echo "yes"
# missing fi # โ syntax error
# Don't mix [ ] and [[ ]]
if [[ $a -gt 0 ] && [ $b -gt 0 ]]; then # โ ๏ธ works but inconsistent
# Don't forget spaces around [ ]
if [$a -gt 0]; then ...; fi # โ syntax error
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
No spaces in [ ] | Syntax error | [ "$a" -gt 0 ] |
Missing fi | Script fails | Add fi |
| Unquoted variable | Breaks on empty | Quote: "$var" |
= for numbers | String compare | Use -eq |
-eq for strings | Error | Use = |
Unescaped > | Redirection | \> or [[ ]] |
elif after else | Unreachable | Reorder |
| Empty variable | [: =: unexpected operator | Quote |
Real-World Examples
1. Classify a Number
#!/bin/bash
read -p "Enter a number: " num
if [ "$num" -lt 0 ]; then
echo "The number is negative."
elif [ "$num" -eq 0 ]; then
echo "The number is zero."
else
echo "The number is positive."
fi
2. Arithmetic Calculator
#!/bin/bash
num1=10
num2=5
echo "Sum: $((num1 + num2))"
echo "Difference: $((num1 - num2))"
echo "Product: $((num1 * num2))"
echo "Quotient: $((num1 / num2))"
echo "Remainder: $((num1 % num2))"
[ Sum: 15 ]
[ Difference: 5 ]
[ Product: 50 ]
[ Quotient: 2 ]
[ Remainder: 0 ]
3. Both Positive
#!/bin/bash
num1=10
num2=5
if [ $num1 -gt 0 ] && [ $num2 -gt 0 ]; then
echo "Both numbers are positive."
else
echo "At least one number is not positive."
fi
[ Both numbers are positive. ]
4. Empty String Check
#!/bin/bash
string1=""
if [ -z "$string1" ]; then
echo "String is empty."
else
echo "String is not empty."
fi
[ String is empty. ]
5. File Exists Check
#!/bin/bash
file="$1"
if [ -f "$file" ]; then
echo "File exists"
wc -l "$file"
else
echo "File not found"
exit 1
fi
6. Directory Check
#!/bin/bash
dir="$1"
if [ -d "$dir" ]; then
echo "$dir is a directory"
ls "$dir" | wc -l
fi
7. Grade Calculator
#!/bin/bash
read -p "Grade: " g
if [ "$g" -ge 90 ]; then echo "A"
elif [ "$g" -ge 80 ]; then echo "B"
elif [ "$g" -ge 70 ]; then echo "C"
elif [ "$g" -ge 60 ]; then echo "D"
else echo "F"
fi
8. Age Check
#!/bin/bash
read -p "Age: " age
if [ "$age" -ge 18 ]; then
echo "Adult"
else
echo "Minor"
fi
9. Login Check
#!/bin/bash
read -p "Username: " user
if [ "$user" = "admin" ]; then
echo "Welcome, admin"
elif [ "$user" = "guest" ]; then
echo "Welcome, guest"
else
echo "Unknown user"
fi
10. Regex Email Validation
#!/bin/bash
read -p "Email: " email
if [[ "$email" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
echo "Valid email"
else
echo "Invalid email"
fi
11. Glob Pattern Match
#!/bin/bash
for file in *; do
if [[ "$file" == *.txt ]]; then
echo "Text file: $file"
fi
done
12. Case Menu
#!/bin/bash
echo "1) Start"
echo "2) Stop"
echo "3) Quit"
read -p "Choice: " c
case "$c" in
1) echo "Starting..." ;;
2) echo "Stopping..." ;;
3) exit 0 ;;
*) echo "Invalid choice" ;;
esac
13. Command Available Check
#!/bin/bash
if command -v git > /dev/null 2>&1; then
echo "git is installed"
else
echo "git is not installed"
fi
14. Multiple Conditions
#!/bin/bash
read -p "Age: " age
read -p "Member (y/n): " member
if [ "$age" -ge 18 ] && [ "$member" = "y" ]; then
echo "Access granted"
else
echo "Access denied"
fi
15. Negation
#!/bin/bash
if ! [ -f /etc/passwd ]; then
echo "Missing critical file!"
exit 1
fi
echo "File exists"
16. Validate Numeric Input
#!/bin/bash
read -p "Number: " n
if [[ "$n" =~ ^[0-9]+$ ]]; then
echo "Valid: $n"
else
echo "Not a number"
fi
17. Yes/No Prompt
#!/bin/bash
read -p "Continue? (y/n): " ans
if [[ "$ans" =~ ^[Yy]$ ]]; then
echo "Continuing"
else
echo "Cancelled"
fi
18. Disk Usage Warning
#!/bin/bash
usage=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$usage" -gt 80 ]; then
echo "WARNING: disk at ${usage}%"
fi
19. HTTP Status Check
#!/bin/bash
status=$(curl -s -o /dev/null -w "%{http_code}" https://example.com)
if [ "$status" -eq 200 ]; then
echo "Site is up"
else
echo "Site returned $status"
fi
20. Full Menu with Loop
#!/bin/bash
while true; do
echo "1) Date"
echo "2) Uptime"
echo "3) Quit"
read -p "Choose: " choice
case "$choice" in
1) date ;;
2) uptime ;;
3) break ;;
*) echo "Invalid" ;;
esac
done
Visual: if / elif / else Flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ if / elif / else โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ if [ condition1 ]; then โ โ
โ โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโดโโโโโโโโ โ
โ true false โ
โ โ โ โ
โ โผ โผ โ
โ run block1 โโโโโโโโโโโโโโโโโโโโโ โ
โ โ elif [ cond2 ]; โ โ
โ โโโโโโโโโโฌโโโโโโโโโโโ โ
โ โโโโโโโโโดโโโโโโโโ โ
โ true false โ
โ โ โ โ
โ โผ โผ โ
โ run block2 โโโโโโโโโโโโโโ โ
โ โ else โ โ
โ โ run block3 โ โ
โ โโโโโโโโโโโโโโ โ
โ โ
โ fi โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Visual: [ ] vs [[ ]]
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ [ ] (POSIX test) โ
โ โ
โ [ "$a" -gt 0 ] โ
โ โข Requires quotes around variables โ
โ โข No && or || inside โ
โ โข No regex or glob matching โ
โ โข Works in all shells โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ [[ ]] (bash extended test) โ
โ โ
โ [[ $a -gt 0 && $b -gt 0 ]] โ
โ โข No quotes needed โ
โ โข && and || work inside โ
โ โข Regex with =~ โ
โ โข Glob patterns with == โ
โ โข Bash, ksh, zsh only โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Concept | Syntax | Example |
|---|---|---|
| Simple if | if [ cond ]; then ...; fi | if [ -f f ]; then ...; fi |
| If/else | if [ c ]; then ...; else ...; fi | |
| Elif | if [ c1 ]; then ...; elif [ c2 ]; then ...; fi | |
| Negation | if ! cmd; then ...; fi | if ! [ -f f ]; then ...; fi |
| Numeric compare | -eq -ne -gt -ge -lt -le | [ $a -gt 0 ] |
| String compare | = != -z -n | [ "$s" = "hi" ] |
| File test | -e -f -d -r -w -x -s | [ -f "$file" ] |
| Logical | && || ! | [ $a -gt 0 ] && [ $b -gt 0 ] |
| Arithmetic | $((...)) | $((a + b)) |
| Extended test | [[ ... ]] | [[ $a -gt 0 && $b -gt 0 ]] |
| Regex | [[ $s =~ pat ]] | [[ $email =~ @.*\.com ]] |
| Case | case ... esac | case $1 in start) ...;; esac |
Key takeaways:
iftests the exit status of a command โ0is true, non-zero is false- Use
[ ... ]for POSIX portability,[[ ... ]]for bash power - Always quote variables in
[ ]โ unquoted empties cause errors - Use
-eq,-ne,-gtetc. for numbers;=,!=for strings - Use
-zfor empty and-nfor non-empty strings - Use
-f,-d,-xto test files - Combine conditions with
&&,||,!โ or inside[[ ]] - Use
casewhen matching one value against many patterns - Arithmetic is integer-only in
$((...))โ usebcorawkfor floats - Always provide an
elseor handle the missing case explicitly - Test in order โ put the most specific condition first
- Don’t forget
fiand spaces around[ ]
Remember: if is the decision-maker of your script. Use it to check numbers, strings, files, and command results. Prefer [[ ]] in bash, but quote everything in [ ]. Use -eq for numbers and = for strings โ mixing them is a common bug. When you have many branches on the same value, case is cleaner. And always think about the “what if it’s empty?” case โ that’s where most shell scripts break.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!