Today we will complete Task8 of our 90daysofDevOps challenge where we have to explore as per below. 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. Your task is to create a bash script with comments explaining what the script does.
Example:
#!/bin/bash
# This is a comment
echo "Hi, Welcome to Challenge 1" # This prints a message to the terminal
Task 2: Echo
The echo command is used to display messages on the terminal. Your task is to create a bash script that uses echo to print a message of your choice.
Example:
#!/bin/bash
# This script prints a welcome message
echo "Welcome to Day 8 of the 90 days of DevOps Challenge!"
Task 3: Variables
Variables in bash are used to store data and can be referenced by their name. Your task is to create a bash script that declares variables and assigns values to them.
Example:
#!/bin/bash
# Declaring variables
a=4
name="Tanmaya"
# Using the variables
echo "$a, $name!"
Task 4: Using Variables
Now that you have declared variables, let's use them to perform a simple task. Create a bash script that takes two variables (numbers) as input and prints their sum using those variables.
Example:
#!/bin/bash
# Declaring variables
no1=20
no2=30
# Calculating the sum
sum=$((no1 + no2))
# Displaying the result
echo "The sum of $no1 and $no2 is $sum"
Task 5: Using Built-in Variables
Bash provides several built-in variables that hold useful information. Your task is to create a bash script that utilizes 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. Your task is to create a bash script that utilizes wildcards to list all the files with a specific extension in a directory.
Example:
#!/bin/bash
# Listing all .txt files in the current directory
echo "All text files in the current directory starting with fi:"
ls fi*.txt #Here fi*.txt represents that file name starts from fi but can ecnd with anything and with a .txt extension.
Task 7: Real Life Application
#!/bin/bash
# This is a comment: Define a user-defined variable
greeting="Hello, Welcome to the World of Shell Scripting"
# Use a built-in variable: The current user's home directory
HOME_DIR=$HOME
# Print the values of the variables
echo "User-defined variable greeting: $greeting" # $ is used to retrieve value
# of a variable.
echo "Built-in variable HOME (Home Directory): $HOME_DIR"
# Use a wildcard to list all text files in the current directory
echo "Listing all .txt files in the current directory:"
for file in *.txt
do
echo "Found text file: $file"
done
# A simple example of using a wildcard and variable together
FILE_PREFIX="example"
echo "Listing all files starting with $FILE_PREFIX:"
for file in $FILE_PREFIX*
do
echo "Found file: $file"
done
Conclusion
Today, we've covered the basics of shell scripting. These are the essentials of shell scripting, including comments for clarity, variables (both user-defined and built-in) for data storage, and the echo
command for output. Additionally, we saw the use of wildcards to efficiently match and process multiple files based on patterns. By understanding and utilizing these fundamental concepts, you can create more dynamic, readable, and effective scripts for a wide range of tasks and workflows, thereby enhancing your scripting proficiency and automation capabilities. Happy scripting!
Feel free to reach me out for any queries :) !!!