Files
linux.softwareshinobi.com/landing/docs/Bash-Scripts/010-bash-conditionals.md
Software Shinobi 7d9171c854
All checks were successful
learn org at code.softwareshinobi.com/linux.softwareshinobi.com/pipeline/head This commit looks good
reworking content
2025-06-19 10:03:08 -04:00

4.3 KiB

Conditionals

You've mastered conditional expressions. Now, let's put them to work using if, if-else, elif, and case statements to control your script's flow.


If Statements

The if statement executes commands only if a condition is true.

if [[ some_test ]]
then
    <commands>
fi

Example: Prompt for a name, then check if it's empty.

touch if_example.sh

Open if_example.sh and add:

#!/usr/bin/env bash

read -p "What is your name? " name

if [[ -z "${name}" ]]; then # Check if name is empty
    echo "Please enter your name!"
fi

Save and exit. Make it executable and run:

chmod +x if_example.sh
./if_example.sh

If-Else Statements

Use else to define actions when the if condition is false.

Create if_else_example.sh:

touch if_else_example.sh

Open if_else_example.sh and add:

#!/usr/bin/env bash

read -p "What is your name? " name

if [[ -z "${name}" ]]; then
    echo "Please enter your name!"
else
    echo "Hi there ${name}!"
fi

Save, make executable, and run.

Admin/Root Checks

Check user roles. For admin verification:

Create admin_check.sh:

touch admin_check.sh

Open admin_check.sh and add:

#!/usr/bin/env bash

admin="Dev Team Six"

read -p "Enter your username: " username

if [[ "${username}" == "${admin}" ]]; then
    echo "You are the admin user!"
else
    echo "You are NOT the admin user!"
fi

Save, make executable, and run.

To prevent a script from running as root (EUID is 0 for root):

Create root_check.sh:

touch root_check.sh

Open root_check.sh and add:

#!/usr/bin/env bash

if [[ $EUID -eq 0 ]]; then # Check if current user ID is 0 (root)
    echo "This script should not be run as root. Exiting."
    exit 1 # Exit with error code
fi
echo "Script running as non-root user."

Save, make executable, and run (test as root and non-root).

Combine conditions for stricter control. This example ensures the user is neither admin nor root.

Create combined_check.sh:

touch combined_check.sh

Open combined_check.sh and add:

#!/usr/bin/env bash

admin_user="Dev Team Six"

read -p "Enter your username: " username

if [[ "${username}" != "${admin_user}" || $EUID -ne 0 ]]; then # OR operator
    echo "You are not the admin or root user. Proceeding safely."
else
    echo "You are an admin or root user. Exercise caution."
fi

Save, make executable, and run.

Elif Statements

For multiple, mutually exclusive conditions, use elif.

Create elif_example.sh:

touch elif_example.sh

Open elif_example.sh and add:

#!/usr/bin/env 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

Save, make executable, and run.

Case Statements

Simplify complex conditionals with case when you have multiple choices.

The case syntax:

case $variable_to_test in
  pattern_1)
    commands_for_pattern_1
    ;;
  pattern_2|pattern_3) # Multiple patterns with |
    commands_for_pattern_2_or_3
    ;;
  *) # Default case (catch-all)
    default_commands
    ;;
esac

Key points: case starts the statement, in follows the variable. Patterns end with ). Commands are terminated by ;;. * is the default. esac closes the statement.

Example: Check car brand and factory location.

Create car_brand_case.sh:

touch car_brand_case.sh

Open car_brand_case.sh and add:

#!/usr/bin/env bash

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

case $car in

  Tesla)
    echo "${car}'s factory is in the USA."
    ;;

  BMW | Mercedes | Audi | Porsche)
    echo "${car}'s factory is in Germany."
    ;;

  Toyota | Mazda | Mitsubishi | Subaru)
    echo "${car}'s factory is in Japan."
    ;;

  *)
    echo "${car} is an unknown car brand."
    ;;

esac

Save, make executable, and run.


Conclusion

Practice these conditional structures. Modify the examples. Mastering flow control is critical for effective scripting.