Variables, Branching and Loops

Fundamentals of Variables, Conditional Branching, and Loops

Objectives:

  • Understand how to declare and use variables in Bash.

  • Learn how to use conditional statements (if, else, elif).

  • Explore loops (for, while) for iterating through data.

  • Apply these concepts in a real-world example.


2.1 Variables in Bash

Declaring Variables

In Bash, you can declare a variable by simply assigning a value to it. There is no need to specify the variable type.

# Example of variable declaration
name="Kunal"
age=30

Note: Do not add spaces around the = sign when assigning values.

Accessing Variables

To access the value of a variable, use the $ symbol:

echo "My name is $name and I am $age years old."

2.2 Conditional Branching

Conditional statements allow you to execute different actions based on certain conditions.

Using if, else, and elif

The syntax for conditional branching is as follows:

if [ condition ]; then
    # code to execute if condition is true
elif [ another_condition ]; then
    # code to execute if another_condition is true
else
    # code to execute if none of the above conditions are true
fi

Example: Checking Age

if [ $age -ge 18 ]; then
    echo "$name is an adult."
else
    echo "$name is a minor."
fi

2.3 Loops in Bash

Loops allow you to execute a block of code multiple times.

Using for Loops

A for loop can iterate over a list of items.

for i in {1..5}; do
    echo "Iteration $i"
done

Using while Loops

A while loop continues as long as a condition is true.

count=1
while [ $count -le 5 ]; do
    echo "Count: $count"
    ((count++))
done

2.4 Real-World Example: User Registration

Let’s create a simple script that simulates a user registration process. The script will collect user information, check if the user is old enough to register, and provide feedback.

Example Script: user_registration.sh

#!/bin/bash

# User Registration Script

# Collect user information
echo "Enter your name:"
read name

echo "Enter your age:"
read age

# Check if the user is eligible to register
if [ $age -ge 18 ]; then
    echo "Welcome, $name! You are eligible to register."
else
    echo "Sorry, $name. You must be at least 18 years old to register."
    exit 1
fi

# Simulate account creation
echo "Creating your account..."
sleep 2  # Simulating a delay for account creation
echo "Account for $name created successfully!"

# Example of a loop to show registration steps
echo "Registration Steps:"
for step in "Fill out personal details" "Choose a username" "Set a password" "Confirm your registration"; do
    echo "- $step"
done

echo "Thank you for registering, $name!"

https://github.com/bc0de0/bash-scripts/blob/master/user_registeration_sh

How to Run the Script

  1. Make the script executable:

    chmod +x user_registration.sh
  2. Run the script:

    ./user_registration.sh

2.5 Summary

In this chapter, you learned about:

  • Variables in Bash and how to declare and access them.

  • Conditional statements for making decisions based on conditions.

  • Loops for executing code multiple times.

  • A real-world example that combines these concepts in a user registration scenario.

Exercises:

  1. Modify the user registration script to include a username and validate its length.

  2. Add a loop to allow the user to register multiple accounts until they choose to exit.


Feel free to adjust any parts of the chapter or the example to better fit your teaching style or audience! Let me know if you need any further details or modifications.

Last updated