Shell Scripting Challenge

Shell Scripting Challenge

  1. Task 1: Comments

In bash scripts, comments are used to add explanatory notes or disable certain lines of code. Create a bash script with comments explaining what the script does.

#!/bin/bash

# This script checks if a specified directory exists, and if it does,
# it creates a backup of the directory by copying it to a backup location.

# Define the source directory to be backed up
SOURCE_DIR="/path/to/source_directory"  # Replace with the actual directory path

# Define the destination directory for the backup
BACKUP_DIR="/path/to/backup_directory"  # Replace with the actual backup directory path

# Check if the source directory exists
if [ -d "$SOURCE_DIR" ]; then
  # If the source directory exists, create a backup
  echo "Source directory exists. Creating backup..."

  # Use the cp command to copy the source directory to the backup directory
  cp -r "$SOURCE_DIR" "$BACKUP_DIR"

  # Print a success message
  echo "Backup created successfully at $BACKUP_DIR"
else
  # If the source directory does not exist, print an error message
  echo "Error: Source directory does not exist."
fi

  1. Task 2 : Echo

The echo command is used to display messages on the terminal. Create a bash script that uses echo to print a message of your choice.

#!/bin/bash

# This script prints a message to the terminal using the echo command.

# Define the message to be printed
MESSAGE="Hello, welcome to my bash script!"

# Use echo to print the message
echo "$MESSAGE"

  1. Task 3 : Variables

    Variables in bash are used to store data and can be referenced by their name. Create a bash script that declares variables and assigns values to them.

#!/bin/bash

# This script demonstrates how to declare variables and assign values to them in bash.
# Declare and assign values to variables
GREETING="Hello"
NAME="Ojas"
AGE=23
CITY="Mumbai"

# Print the values of the variables using echo
echo "$GREETING, my name is $NAME."
echo "I am $AGE years old."
echo "I live in $CITY."

  1. 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.

#!/bin/bash

# This script takes two numbers as input and prints their sum.

# Declare variables and assign values to them
NUMBER1=5  # You can change this value
NUMBER2=10 # You can change this value

# Calculate the sum of the two numbers
SUM=$((NUMBER1 + NUMBER2))

# Print the sum
echo "The sum of $NUMBER1 and $NUMBER2 is: $SUM"

  1. Task 5 : Using Built-in Variables

    Bash provides several built-in variables that hold useful information. Create a bash script that utilizes at least three different built-in variables to display relevant information.

#!/bin/bash

# This script demonstrates the use of built-in variables in bash.

# Display the name of the script
echo "Script name: $0"

# Display the number of arguments passed to the script
echo "Number of arguments: $#"

# Display the current process ID
echo "Current process ID: $$"

# Display the user executing the script
echo "User executing the script: $USER"

# Display the current working directory
echo "Current working directory: $PWD"

  1. Task 6 : Wildcards

    Wildcards are special characters used to perform pattern matching when working with files. Create a bash script that utilizes wildcards to list all the files with a specific extension in a directory.

#!/bin/bash

# This script lists all files with a specific extension in a directory.

# Specify the directory to search (you can change this to any directory)
DIRECTORY="/path/to/directory"  # Replace with the actual directory path

# Specify the file extension to look for
EXTENSION="txt"  # Change this to the desired file extension

# Use wildcards to list all files with the specified extension in the directory
echo "Listing all .$EXTENSION files in the directory $DIRECTORY:"
ls "$DIRECTORY"/*.$EXTENSION

# Alternatively, you can use a loop to list the files
echo "Using a loop to list the files:"
for file in "$DIRECTORY"/*.$EXTENSION; 
do
  echo "$file"
done

Final Script

#!/bin/bash

# Declare variables and assign values to them
fname="Ojas"
lname="Jawale"

# Printing value of variable
echo "Hii, $fname $lname!!"

# Take input as in number and print sum of them
num1=5
num2=10

echo "Sum of $num1 and $num2 is: $((num1+num2))"

echo "Script $0 is running by user: $USER"
echo "Process ID is: $$"
# Output

Hii, Ojas Jawale!!
Sum of 5 and 10 is: 15
Script ./bash.sh is running by user: shuhari
Process ID is: 772

Thanks to everyone and follow me for more amazing content :)