In this blog, I'll walk you through the Shell Scripting Challenge I completed on Day 8 of the #90DaysOfDevOps challenge. The challenge focused on essential bash scripting skills, including comments, variables, echo, and built-in variables. Let's dive into the tasks I worked on and the scripts I wrote to accomplish each task.
1. Comments in Bash Scripts
In bash scripts, comments are used to explain the code or temporarily disable parts of the script. The script below demonstrates how to add single-line and multi-line comments in bash.
#!/bin/bash
# This is a single-line comment
<<Info
This is a multi-line comment.
You can describe the script or add important notes here.
Info
Explanation:
The
#
is used for single-line comments.The
<<Info
andInfo
syntax is used for multi-line comments, often called a "here document."
2. Using Echo Command
The echo
command in bash is used to display messages to the terminal. Here's a simple script using echo
to print a message:
#!/bin/bash
echo "Welcome to Day-08 of the 90DaysOfDevOps challenge!"
Explanation:
echo
is used to print the string"Welcome to Day-08 of the 90DaysOfDevOps challenge!"
to the terminal.
3. Declaring Variables
Variables in bash are used to store data. Here's a script that declares and assigns values to variables:
#!/bin/bash
sumation=0
echo "Variable 'sumation' is initialized with value: $sumation"
Explanation:
sumation=0
initializes the variablesumation
with a value of0
.echo
prints the value of the variable.
4. Using Variables to Perform Tasks
This script takes two numbers as input, calculates their sum, and prints the result:
#!/bin/bash
read -p "Enter number 1: " num1
read -p "Enter number 2: " num2
sumation=$(( num1 + num2 ))
echo "Summation of $num1 + $num2 = $sumation"
Explanation:
read
is used to take user input for two numbers (num1
andnum2
).The sum of the two numbers is calculated and stored in the variable
sumation
, which is then printed usingecho
.
5. Using Built-in Variables
Bash provides several built-in variables that hold useful information. Here’s a script that uses a few of them:
#!/bin/bash
echo "Today's date is: $(date)"
echo "We are currently in directory: $(pwd)"
echo "User logged in: $(whoami)"
echo "Home directory: $HOME"
Explanation:
$(date)
prints the current date and time.$(pwd)
prints the current working directory.$(whoami)
prints the current user logged in.$HOME
prints the home directory of the current user.
6. Using Wildcards to List Files
Wildcards in bash allow you to match patterns in filenames. This script uses wildcards to list all .sh
files in a specified directory:
#!/bin/bash
read -p "Enter directory path to list .sh files: " sh_dir
cd ${sh_dir} && ls *.sh
Explanation:
The
read
command takes the directory path from the user.The
cd
command changes to the specified directory, andls *.sh
lists all.sh
files in that directory.
7. Whole Script Solution
Below is the entire script, combining all the tasks for Day 8:
#!/bin/bash
<<Info
Author : Amitabh Soni
Date : 1/12/24
Description : This script will store all tasks for Day 8 Task: Shell Scripting Challenge of 90DaysOfDevOps challenge
Info
# Task 1: Comments
# This # is used to create a single-line comment.
# The <<Info and Info are used for a multiline comment.
# Task 2: Echo Command
# The echo command is used to display text on the terminal.
echo "Welcome to Day-08 of 90DaysOfDevOps challenge"
# Task 3: Variable
# Initializing a variable 'sumation' and setting its value to 0
sumation=0
# Task 4: Using Variables
# The read command prompts the user to input a value.
# The values entered by the user are stored in the variables num1 and num2.
read -p "Enter number 1 : " num1
read -p "Enter number 2 : " num2
# Summing the two numbers entered by the user and storing the result in the 'sumation' variable.
sumation=$(( num1 + num2 ))
# Displaying the result of the summation.
echo "Summation of $num1 + $num2 : $sumation"
# Task 5: Using Built-in Variables
# Using the built-in 'date' command to display the current date and time.
echo "Today's date is : $(date)"
# Using the 'pwd' command to display the current working directory.
echo "In present we are in $(pwd)"
# Using the 'whoami' command to display the current user logged in.
echo "User is $(whoami)"
# Using the built-in variable $HOME to display the current user's home directory.
echo "Home dir : $HOME"
# Task 6: Wildcards
# The read command prompts the user to input a directory path.
# Then the script changes to that directory and lists all files with the .sh extension using the 'ls' command and wildcard.
read -p "Enter dir path to print all .sh files in that dir : " sh_dir
cd ${sh_dir} && ls *.sh
Conclusion
This Shell Scripting Challenge provided a solid understanding of bash scripting fundamentals, including working with comments, variables, and built-in commands. It also gave hands-on experience with user input and pattern matching using wildcards. These skills are essential for automating tasks in a DevOps pipeline, and I look forward to applying them in future projects.
Stay tuned for more updates as I progress through the #90DaysOfDevOps challenge!
Feel free to check out my LinkedIn for more updates and my journey through the DevOps learning path!