Day 8 Task: Shell Scripting Challenge

Day 8 Task: Shell Scripting Challenge

Welcome to Day 8 of our 90-day DevOps journey! Today, we’re diving into shell scripting—a fundamental skill for any DevOps engineer. Shell scripting allows us to automate tasks, making our workflows more efficient and reliable. Let's break down the tasks for today:

Task 1: Comments

In bash scripts, comments are used to add explanatory notes or disable certain lines of code. Comments start with the # symbol. They are ignored by the shell and are only meant for humans reading the script.

Example:

#!/bin/bash
# This is a comment
echo "Hello, World!" # This prints a message to the terminal

Task 2: Echo

The echo command is used to display messages on the terminal. It's a simple yet powerful tool for outputting text and variables.

Example:

#!/bin/bash
# This script prints a welcome message
echo "Welcome to Day 8 of the DevOps Challenge!"

Task 3: Variables

Variables in bash are used to store data and can be referenced by their name. They help in making scripts dynamic and reusable.

Example:

#!/bin/bash
# Declaring variables
greeting="Hello"
name="Priyadar"
# Using the variables
echo "$greeting, $name!"

Task 4: Using Variables

Now that you have declared variables, let's use them to perform a simple task. This script takes two variables (numbers) as input and prints their sum using those variables.

Example:

#!/bin/bash
# Declaring variables
num1=5
num2=10
# Calculating the sum
sum=$((num1 + num2))
# Displaying the result
echo "The sum of $num1 and $num2 is $sum"

Task 5: Using Built-in Variables

Bash provides several built-in variables that hold useful information. Let's utilize at least three different built-in variables to display relevant information.

Example:

#!/bin/bash
# Displaying built-in variables
echo "Script name: $0"
echo "First argument: $1"
echo "Number of arguments: $#"

Task 6: Wildcards

Wildcards are special characters used to perform pattern matching when working with files. They help in listing or processing multiple files at once.

Example:

#!/bin/bash
# Listing all .txt files in the current directory
echo "Text files in the current directory:"
ls *.txt

Real-life Application

Let's create a practical script that utilizes all these concepts. Imagine you are automating a task where you need to process text files in a directory, add a header to each file, and then list the processed files.

Example:

#!/bin/bash
# Adding comments to explain the script
# This script processes text files by adding a header and lists the processed files

# Define the header
header="Processed by Priyadar's script"

# Loop through all .txt files in the current directory
for file in *.txt
do
  # Add the header to each file
  echo "$header" | cat - "$file" > temp && mv temp "$file"
  echo "Processed: $file"
done

# List all processed files
echo "All processed text files:"
ls *.txt

Conclusion

Today, we've covered the basics of shell scripting, including comments, echo commands, variables, built-in variables, and wildcards. By mastering these fundamentals, you can automate a wide range of tasks, making your workflow more efficient and reliable. As you continue on this DevOps journey, remember that practice is key. Keep experimenting with scripts, and soon you'll find yourself creating complex automation solutions with ease. Happy scripting!

Learn More

Check out this YouTube tutorial for a deeper dive into shell scripting:

Shell Scripting Tutorial for Beginner