Day 8 of 100 days : Mastering Advanced Shell Scripting

Munilakshmi G JMunilakshmi G J
4 min read

Let’s Dive Deeper into Shell Scripting in DevOps

Welcome to Day 8! Today, we’re tackling some powerful shell scripting techniques. These are essential for making scripts dynamic, interactive, and smart. We’ll explore command-line options, control flow, loops, functions, and more! By the end, you’ll have a toolkit for writing scripts that can handle all kinds of real-world tasks. Ready? Let’s jump in.


1. Handling Command-Line Options with getopts

Imagine you’re creating a script that requires specific user inputs like a username and password. Instead of prompting each time, we can use flags for inputs, e.g., -u for username and -p for password. This is where getopts comes in handy!

Try running the example below in a new script file, and then test it by running the command:

#!/bin/bash
while getopts u:p: option; do
  case "${option}" in
    u) USERNAME=${OPTARG} ;;
    p) PASSWORD=${OPTARG} ;;
    *) echo "Usage: $0 -u <username> -p <password>" ;;
  esac
done
echo "Username: $USERNAME, Password: $PASSWORD"

Test It Out

Run:

./script_name.sh -u yourusername -p yourpassword

Notice how easily you can handle different arguments, making your script more user-friendly!


2. Control Flow with if, else, and elif

Conditional statements let you control what happens based on certain conditions—just like setting permissions for different users.

Let’s create a script that checks if the inputted username is “admin” or “user” and gives a response based on that.

#!/bin/bash
read -p "Enter username: " username
if [ "$username" == "admin" ]; then
  echo "Welcome, admin!"
elif [ "$username" == "user" ]; then
  echo "Welcome, user!"
else
  echo "Unknown user."
fi

Try It Out

Run the script and input various usernames to see how it responds. You can build on this by adding more conditions or creating additional messages for each role!


3. Loops in Shell: for, while, and until

Loops are ideal for repeating actions without writing the same code over and over. Let’s look at each type of loop with examples:

  • for Loop: Executes a set number of times.

      for i in {1..5}; do
        echo "Number: $i"
      done
    
  • while Loop: Runs as long as a condition is true.

      i=1
      while [ $i -le 5 ]; do
        echo "Count: $i"
        ((i++))
      done
    
  • until Loop: Executes until a condition becomes true.

      i=5
      until [ $i -eq 0 ]; do
        echo "Reverse Count: $i"
        ((i--))
      done
    

Try It Out

Run each loop example and watch how they count in different ways. Loops are incredibly useful when working with file processing or managing tasks in bulk.


4. Functions: Reusable Code Blocks

Functions allow you to reuse a block of code, making scripts modular and manageable.

Let’s write a simple function that adds two numbers. Try this example:

#!/bin/bash
add_numbers() {
  echo "Sum: $(( $1 + $2 ))"
}
add_numbers 5 10

Test It Out

Run the script. Functions let you pass parameters and call them repeatedly without retyping code. Imagine using this in a script that calculates multiple values dynamically!


5. The eval Command: Dynamic Command Execution

eval is like a powerful way to run commands stored as variables. Let’s see this in action:

cmd="echo Hello World"
eval $cmd

Try It Out

This runs the echo command stored in the variable. eval can be used for creating flexible scripts where command structure might vary based on inputs or parameters.


6. The let Command: Simplifying Arithmetic

In scripting, calculations are common, and let makes them straightforward. Here’s a simple example:

#!/bin/bash
let result=5+3
echo "Result: $result"

Try It Out

Run it to see the sum printed. let is especially handy for counters or any arithmetic needed in your script logic.


Now you’re all set with advanced shell scripting techniques! These tools will let you handle a wide variety of tasks in DevOps scripting, from user input handling to looping and arithmetic. Keep experimenting, and you’ll master these techniques in no time.

0
Subscribe to my newsletter

Read articles from Munilakshmi G J directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Munilakshmi G J
Munilakshmi G J

"Aspiring DevOps Engineer on a 100-day journey to master the principles, tools, and practices of DevOps. Sharing daily insights, practical lessons, and hands-on projects to document my path from beginner to proficient. Passionate about continuous learning, automation, and bridging the gap between development and operations. Join me as I explore the world of DevOps, one day at a time!"