๐Ÿ“œ Continuation of Shell Scripting: Working with Conditions and Arguments

ยท

3 min read

Hey everyone! Welcome back to my DevOps blog. Today, weโ€™re diving deeper into shell scripting by exploring how to use conditions and arguments to make your scripts more dynamic and powerful. Letโ€™s break down the key concepts and see some practical examples.


๐Ÿ” 1. IF Condition

The if condition is used to execute commands based on whether a condition is true or not. If the condition is met, the script performs the specified actions; otherwise, it stops.

Example: Let's create a script named raees.sh that checks if a rank is equal to 1 and provides a corresponding message.

vim raees.sh

Script Content:

#!/bin/bash

echo "Raees ki rank $1"

if [ $1 -eq 1 ]; then
  echo "Raees is Topper"
fi

Running the Script:

./raees.sh 1 2 3 4

In this example, if the first argument $1 equals 1, it will output "Raees is Topper".


๐Ÿ”„ IF-ELSE Condition

The if-else condition allows you to execute different commands based on whether the condition is true or false.

Example: Update the raees.sh script to check multiple ranks and provide appropriate messages.

vim raees.sh

Script Content:

#!/bin/bash

echo "Raju ki rank $1"
echo "Farhan ki rank $2"
echo "Rancho ki rank $3"
echo "Chatur ki rank $4"

if [ $1 -eq 1 ]; then
  echo "Raju is Topper"
elif [ $2 -eq 1 ]; then
  echo "Farhan is Topper"
elif [ $3 -eq 1 ]; then
  echo "Rancho is Topper"
elif [ $4 -eq 1 ]; then
  echo "Chatur is Topper"
fi

Running the Script:

./raees.sh 1 2 3 4

This script checks each argument to determine which rank is the topper and outputs the corresponding message.


๐Ÿ” FOR Loop

The for loop is useful for repeating actions, such as creating multiple users or processing a list of items.

Example: Create a script to add three users using a for loop.

vim raees.sh

Script Content:

#!/bin/bash

for ((i=1; i<=3; i++))
do
  read -p "Enter username: " username
  read -p "Enter password: " password
  sudo useradd -m $username -p $password
  echo "User $username created successfully"
done

Running the Script:

./raees.sh

This script prompts for a username and password three times and creates three users.


๐Ÿ”ง DRY Principle (Donโ€™t Repeat Yourself)

The DRY principle helps avoid code duplication by using functions. Instead of repeating code, you create functions and call them whenever needed.

Example: Refactor the user creation script to use a function.

vim raees.sh

Script Content:

#!/bin/bash

create_user() {
  read -p "Enter username: " username
  read -p "Enter password: " password
  sudo useradd -m $username -p $password
  echo "User $username created successfully"
}

for ((i=1; i<=3; i++))
do
  create_user
done

Running the Script:

./raees.sh

This version uses the create_user function to avoid repeating code.


๐Ÿ”„ CASE Statement

The case statement is useful for handling multiple conditions in a clean and organized way.

Example: Create a script user_management.sh to manage users with options to create or delete them.

vim user_management.sh

Script Content:

#!/bin/bash

show_usage() {
  echo "Welcome to user management"
  echo "To create a user, use 'c' as an argument"
  echo "To delete a user, use 'd' as an argument"
}

case "$1" in
  c)
    echo "Creating user in progress"
    create_user
    ;;
  d)
    echo "Deleting user in progress"
    delete_user
    ;;
  *)
    show_usage
    ;;
esac

Running the Script:

./user_management.sh c

In this script, based on the argument passed (c for create or d for delete), it will perform the appropriate action.


I hope these examples help you understand how to use conditions, loops, and functions in shell scripting. Feel free to try these out and enhance your scripting skills!

For more details and tutorials, check out my blog here and donโ€™t forget to subscribe to my YouTube channel for more updates. ๐Ÿš€

Happy scripting! ๐Ÿ–ฅ๏ธ๐Ÿ’ก

ย