Day 8 Task: Shell Scripting Challenge
Shell scripting in Bash is an essential skill for any development environment. It allows for automating tasks, managing systems efficiently, and performing repetitive tasks with ease. In this guide, we will walk through some fundamental concepts of shell scripting with practical tasks to solidify your understanding.
Task 1: Comments
In bash scripts, comments are used to add explanatory notes or disable certain lines of code.
#!/bin/bash
# First line of the script is the shebang which tells the system how to execute
Task 2: Echo
The echo
command is used to display messages on the terminal. It's a basic but powerful way to interact with the user and display information.
# Task 2: Echo
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.
# Task 3: Variables
variable1="Hello"
variable2="Bash"
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.
#!/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. Your task is to create a bash script that utilizes at least three different built-in variables to display relevant information.
#!/bin/bash
# Displaying built-in variables
echo "current user: $USER"
echo "Home Directory: $HOME"
echo "Shell: $Shell"
echo "Number o0f arguements: $#"
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.
#!/bin/bash
# Listing all .txt files in the current directory
echo "Text files in the current directory:"
ls *.txt
Create a single bash script that completes all the tasks mentioned above.
#!/bin/bash
# First line of the script is the shebang which tells the system how to execute
# Task 2: Echo
echo "Processed by Pooja's script"
# Task 3: Variables
variable1="Hello"
variable2="Bash"
# Task 4: Using Variables
greeting="$variable1, $variable2!"
echo "$greeting Welcome to the world of Bash scripting!"
# Task 5: Using Built-in Variables
echo "My current bash path - $BASH"
echo "Bash version I am using - $BASH_VERSION"
echo "PID of bash I am running - $$"
echo "My home directory - $HOME"
echo "Where am I currently? - $PWD"
echo "My hostname - $HOSTNAME"
# Task 6: Wildcards
echo "Files with .txt extension in the current directory:"
ls *.txt