DevOps 90-Day Challenge Day 8: Learning Shell Scripting Skills

#!/bin/bash

# This script demonstrates various Bash scripting features, with definitions and examples for each.

# ---------------- Task 1: Comments ----------------

# Definition:
# Comments in Bash start with a '#' and are ignored by the interpreter.
# They are used to add notes or disable lines of code for debugging or documentation purposes.

# Example of Comments:
# This is a single-line comment explaining the code.
# The following line will print a message to the terminal.
echo "Task 1: Demonstrating comments."

# ---------------- Task 2: Echo ----------------

# Definition:
# The echo command is used to print text or messages to the terminal.
# It is commonly used to provide feedback to users or display output.

# Example of Echo:
echo "Task 2: Hello, World! Welcome to Bash scripting."

# ---------------- Task 3: Variables ----------------

# Definition:
# Variables in Bash are used to store data values. They can hold strings, numbers, or other data types.
# A variable is declared without a prefix, and its value is assigned using the '=' operator.

# Example of Variables:
greeting="Hello, DevOps Enthusiast!"
year=2024
echo "Task 3: $greeting This is the year $year."

# ---------------- Task 4: Using Variables ----------------

# Definition:
# Variables can be used to store data and perform operations.
# Arithmetic operations can be performed using (( )) or the expr command.

# Example of Using Variables:
num1=50
num2=30
sum=$((num1 + num2))
echo "Task 4: The sum of $num1 and $num2 is $sum."

# ---------------- Task 5: Built-in Variables ----------------

# Definition:
# Bash has several built-in variables that provide information about the script and environment.
# Examples include:
# - $0: Name of the script.
# - $#: Number of arguments passed.
# - $USER: Current username.
# - $PWD: Current working directory.

# Example of Built-in Variables:
echo "Task 5: Script name is $0."
echo "Number of arguments passed: $#."
echo "Current user is $USER."
echo "Current working directory: $PWD."

# ---------------- Task 6: Wildcards ----------------

# Definition:
# Wildcards are special characters used to match patterns in file and directory names.
# Examples:
# - * matches zero or more characters.
# - ? matches a single character.
# - [ ] matches a range of characters.

# Example of Wildcards:
echo "Task 6: Listing all '.sh' files in the current directory:"
ls *.sh

# End of Script

Explanation of Each Task:

Task 1: Comments

Code:

# Comments in Bash start with a '#' and are ignored by the interpreter.
# They are used to explain the code or disable certain lines for debugging.

Explanation:

  • Comments improve the readability of your script by explaining what the code does.

  • They are ignored during script execution and do not affect the output.

  • Useful for documenting logic, purposes, or caveats of specific code sections.


Task 2: Echo

Code:

echo "Task 2: Hello, World! Welcome to Bash scripting."

Explanation:

  • echo prints the text to the terminal.

  • It’s a basic way to provide user feedback or display output.

  • In this example, it displays a welcoming message.


Task 3: Variables

Code:

greeting="Hello, DevOps Enthusiast!"
year=2024
echo "Task 3: $greeting This is the year $year."

Explanation:

  • Variables in Bash store values like strings or numbers.

  • No = spaces when assigning values (e.g., greeting = "text" would result in an error).

  • $variable_name is used to reference the value stored in a variable.

  • In this example:

    • greeting holds a welcome message.

    • year holds the number 2024.

    • The echo command combines these variables into a readable output.


Task 4: Using Variables

Code:

num1=50
num2=30
sum=$((num1 + num2))
echo "Task 4: The sum of $num1 and $num2 is $sum."

Explanation:

  • Variables can hold numbers and be used in calculations.

  • $((expression)) performs arithmetic operations.

  • This task:

    • Declares num1 and num2.

    • Calculates their sum and stores it in sum.

    • Displays the result using echo.


Task 5: Built-in Variables

Code:

echo "Task 5: Script name is $0."
echo "Number of arguments passed: $#."
echo "Current user is $USER."
echo "Current working directory: $PWD."

Explanation:

  • Bash has several predefined or built-in variables that provide system and environment information:

    • $0: The name of the script file.

    • $#: The number of arguments passed to the script when it’s run.

    • $USER: The username of the person executing the script.

    • $PWD: The current directory where the script is being run.

  • This task demonstrates how to access and display these built-in variables.


Task 6: Wildcards

Code:

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

Explanation:

  • Wildcards (*, ?, [ ]) help match patterns when working with files or directories.

  • * matches any number of characters.

  • ls *.sh lists all files in the current directory ending with .sh.

  • This task assumes there are .sh files present in the directory; otherwise, it shows an empty result.

0
Subscribe to my newsletter

Read articles from Shubhranshu Ransingh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shubhranshu Ransingh
Shubhranshu Ransingh