Skip to main content

Command Palette

Search for a command to run...

Day 8 Task: Shell Scripting Challenge

Published
2 min read
A

DevOps | Cloud Practitioner | AWS | GIT | Kubernetes | Terraform | ArgoCD | Gitlab

Here’s a bash script that fulfills all the tasks for Day 8 of the Shell Scripting Challenge, with comments explaining each section.

#!/bin/bash

# Task 1: Comments
# This script demonstrates various basic concepts in bash scripting including comments, echo, variables, using variables, built-in variables, and wildcards.

# Task 2: Echo
# Using the echo command to print a message to the terminal.
echo "Welcome to Day 8 of the Bash Scripting Challenge!"

# Task 3: Variables
# Declaring and assigning values to variables.
name="Amit"
day=8

# Task 4: Using Variables
# Using the variables declared above to print personalized information.
echo "Hello, $name! You are working on Day $day of the challenge."

# Task 4 continued: Adding two variables
# Declare two number variables and calculate their sum
num1=10
num2=25
sum=$((num1 + num2))

# Output the sum
echo "The sum of $num1 and $num2 is $sum."

# Task 5: Using Built-in Variables
# Bash built-in variables include $0, $USER, $SHELL, $PWD, $HOSTNAME, etc.
# Below we will use some of these built-in variables to display information.

# Display the script name (which is stored in $0)
echo "This script is called: $0"

# Display the current logged-in user ($USER)
echo "Current user: $USER"

# Display the shell being used ($SHELL)
echo "Shell being used: $SHELL"

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

# Task 6: Wildcards
# Wildcards are used to match file patterns. The '*' wildcard matches all files.
# We'll use a wildcard to list all '.sh' files in the current directory.

echo "Listing all '.sh' files in the current directory:"
ls *.sh

# End of script
echo "Bash scripting challenge completed!"

Explanation of the Script:

  1. Task 1: Comments: Every section of the script is documented with comments for clarity.

  2. Task 2: Echo: A simple message is printed to the terminal using echo.

  3. Task 3: Variables: Variables name and day are declared and assigned values. These values are then used in the next step.

  4. Task 4: Using Variables: The script calculates the sum of two numbers and outputs the result.

  5. Task 5: Built-in Variables: The script uses bash built-in variables like $0, $USER, $SHELL, and $PWD to display useful information about the script and the system environment.

  6. Task 6: Wildcards: The script lists all .sh files in the current directory using the ls *.sh command, which demonstrates the use of wildcards.

How to Run:

  1. Save this script as challenge_day8.sh.

  2. Give execution permission:

     chmod +x challenge_day8.sh
     or  
     chmod 766 challenge_day0.sh
    
  3. Run the script:

     ./challenge_day8.sh
    

Submission:

Check over github

More from this blog

DevOps Projects and Blog

69 posts