Day 1 of the Bash Scripting Challenge! ๐ #TWSBashBlazeChallenge
๐ Introduction:
Bash scripting is a powerful tool that allows DevOps engineers to automate tasks and perform various operations on their systems. Today, we will cover the basics of bash scripting to get you started on this exciting journey. By the end of this challenge, you will have created a single bash script that completes multiple tasks and demonstrates your understanding of the concepts covered. Let's dive in! ๐ป
Task 1: Comments โ๏ธ
In bash scripts, comments play a vital role in making the code more understandable and explaining its functionality. They begin with the #
symbol. Let's create a bash script with comments explaining what the script does. ๐
#!/bin/bash
# DevOps Engineer: This is a bash script that demonstrates basic concepts of bash scripting.
# Task 1: Comments
# - In this task, we will use comments to explain the script.
# Output a welcome message to the user
echo "Welcome to Day 1 of the Bash Scripting Challenge!"
Task 2: Echo ๐ฃ๏ธ
The echo
command is one of the simplest and most frequently used commands in bash scripting. It is used to display messages on the terminal. Let's create a bash script that uses echo
to print a message of our choice. ๐ข
#!/bin/bash
# DevOps Engineer: Task 2: Echo
# - In this task, we will use the 'echo' command to print a message.
# Output a welcome message to the user
echo "Welcome to Day 1 of the Bash Scripting Challenge!"
Task 3: Variables ๐ฆ
Variables in bash are used to store data and can be referenced by their name. In this task, we will create a bash script that declares variables and assigns values to them. ๐
#!/bin/bash
# DevOps Engineer: Task 3: Variables
# - In this task, we will declare and use variables.
# Declare variables and assign values
engineer_name="Prasad"
engineer_age=23
engineer_country="India"
# Output the values of variables
echo "Name: $engineer_name"
echo "Age: $engineer_age"
echo "Country: $engineer_country"
Task 4: Using Variables ๐๏ธ
Now that we have declared variables, let's use them to perform a simple task. In this task, we will create a bash script that takes two variables (numbers) as input and prints their sum using those variables. โ
#!/bin/bash
# DevOps Engineer: Task 4: Using Variables
# - In this task, we will use variables to perform a calculation.
# Get user input for two numbers
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
# Calculate the sum of the two numbers
sum=$((num1 + num2))
# Output 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. In this task, we will create a bash script that utilizes at least three different built-in variables to display relevant information. ๐งฐ
#!/bin/bash
# DevOps Engineer: Task 5: Using Built-in Variables
# - In this task, we will use built-in variables.
# Output the current user
echo "Current user: $USER"
# Output the hostname
echo "Hostname: $HOSTNAME"
# Output the current working directory
echo "Current directory: $PWD"
Task 6: Wildcards ๐
Wildcards are special characters used to perform pattern matching when working with files. In this task, we will create a bash script that utilizes wildcards to list all the files with a specific extension in a directory. ๐
#!/bin/bash
# DevOps Engineer: Task 6: Wildcards
# - In this task, we will use wildcards to list files.
# Define the target directory
target_directory="/home/suman/devops_docs/"
# List all files with a specific extension in the target directory
echo "Files with the '.txt' extension:"
ls "$target_directory"/*.txt
๐ Checkout GitHub Repository for projects:
๐ github.com/sumanprasad007