๐Ÿš€ Day 8 Task: Shell Scripting Challenge ๐Ÿ–ฅ๏ธ๐Ÿ“œ

๐Ÿš€ Day 8 Task: Shell Scripting Challenge ๐Ÿ–ฅ๏ธ๐Ÿ“œ

ยท

3 min read

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.

Answer: A line in a script or code that is ignored by the system, used to leave notes or explanations. In shell scripts, comments start with #.

#!/bin/bash

#This script print a Welcome message and current date&time

# Print a Welcome message

echo "Welcome to the 90DaysofDevops challenges"

#Print the date and time

echo "The current date and time is $(date)"

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.

Answer: A command used to display text or the value of variables on the screen in the terminal.

#!/bin/bash
# Print a Welcome message
echo "Hello Everyone, Hope you are doing well! Welcome to the 90DaysofDevops challenges"

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.

Answer: Named placeholders used to store and reference data (like text or numbers) in a script or program.

#!/bin/bash
#Script that declares variables and assigns values to them.
echo "What is your name:"
read name
echo "What is this challenge about:"
read challenge
echo "My name is $name and I am participating in $challenge challenge"

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.

Answer:

# Take two variables(numbers)as input and prints their sum using those variables.
echo "Enter the number1:"
read N1
echo "Enter the number2:"
read N2
Sum=$((N1+N2))
echo "The sum of $N1 & $N2 are: $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.

Answer:

#!/bin/bash
#Using different built-in varibles to display relevant information
echo "Current login user is:$USER"
echo "Current working directory is:$PWD"
echo "Current shell is:$SHELL"
echo "Home directory is:$HOME"

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.

Answer: Special characters used to match patterns in filenames or directories, allowing you to work with multiple files at once (e.g., *, ?, []).

#!/bin/bash
echo "Collecting all the files that have .sh extension"
ls *.sh

Happy Learning!

ย