#90DaysOfDevops | Day 8

Rajendra PatilRajendra Patil
3 min read

Welcome to Day 8 of our 90-day DevOps journey! Today, we're diving into shell scripting—an absolutely essential skill for any DevOps engineer! Shell scripting lets us automate tasks, making our workflows super efficient and reliable. Let's get excited and 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="Siddhi"
# 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 siddhi'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

Today, we've learned the basics of shell scripting, including comments, echo commands, variables, built-in variables, and wildcards.As you continue your DevOps journey, remember that practice is crucial. Keep experimenting with scripts, and soon you'll be creating complex automation solutions effortlessly.

Happy scripting!🚀

0
Subscribe to my newsletter

Read articles from Rajendra Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajendra Patil
Rajendra Patil