Day 8 Task: Shell Scripting Challenge

Complete Script: day8_task.sh

#!/bin/bash
# Day 8 Task: Shell Scripting Challenge
# This script demonstrates the use of comments, echo, variables, built-in variables, and wildcards in bash scripting.

# Task 1: Comments
# Comments help explain what each part of the script does.
# They are not executed by the bash shell. This line is a comment.

# Task 2: Echo
# Echo is used to print messages to the terminal.
echo "Hello, welcome to Day 8 of the Bash Scripting Challenge!"  # Prints a greeting message

# Task 3: Variables
# Declaring and assigning variables in bash.
name="Faizan"
age=24
echo "My name is $name and I am $age years old."  # Prints the values stored in variables

# Task 4: Using Variables
# Here we use two variables (numbers) and calculate their sum.
num1=15  # First number
num2=30  # Second number
sum=$((num1 + num2))  # Calculate the sum of num1 and num2
echo "The sum of $num1 and $num2 is: $sum"  # Print the result

# Task 5: Using Built-in Variables
# Bash provides several built-in variables that store useful information.
echo "Script name: $0"  # $0 stores the name of the script
echo "Number of arguments passed: $#"  # $# shows how many arguments were passed
echo "Current directory: $PWD"  # $PWD gives the current working directory path

# Task 6: Wildcards
# Wildcards allow pattern matching in filenames.
# We'll list all files with the '.txt' extension in the current directory.
echo "Listing all .txt files in the current directory:"
ls *.txt  # Lists all files with the '.txt' extension in the current directory

# End of script

Explanation of Each Task

Task 1: Comments

  • Comments are lines that begin with #. They are ignored by the shell and are used to add explanations or disable code.
# This is a comment explaining the next line of code.

Task 2: Echo

  • Echo prints text to the terminal. In the script, we used echo to display a greeting message:
echo "Hello, welcome to Day 8 of the Bash Scripting Challenge!"

Task 3: Variables

  • Variables are used to store data. You can declare variables in bash by assigning them values without spaces around the equal sign (=):
name="Faizan"
age=24
  • We can access variables by prefixing them with $:
echo "My name is $name and I am $age years old."

Task 4: Using Variables

  • Arithmetic Operations in bash can be done by wrapping the operation in $(( )). In this case, we are adding two numbers stored in num1 and num2:
num1=15
num2=30
sum=$((num1 + num2))
  • We then print the result:
echo "The sum of $num1 and $num2 is: $sum"

Task 5: Using Built-in Variables

  • Built-in Variables are predefined variables that store useful information.

    • $0 holds the name of the script.

    • $# gives the number of arguments passed to the script.

    • $PWD holds the current working directory.

echo "Script name: $0"
echo "Number of arguments passed: $#"
echo "Current directory: $PWD"

Task 6: Wildcards

  • Wildcards like * allow you to match patterns in filenames. Here, we list all files in the current directory with a .txt extension:
ls *.txt

This command lists all files that match the pattern *.txt (any file ending with .txt).

How to Run the Script

  1. Create the script file:

    • Open a terminal and create a new file called day8_task.sh:
    nano day8_task.sh
  • Copy the script into this file and save it.
  1. Make the script executable:

    • To run the script, you need to give it executable permissions:
    chmod +x day8_task.sh
  1. Run the script:

    • Execute the script using:
    ./day8_task.sh

Remember, scripting is like solving puzzles—you'll get better with practice, and the possibilities are endless. Keep experimenting, building, and having fun with your scripts!