πŸš€ Shell Scripting Challenge.

πŸš€ Shell Scripting Challenge.

Learn essential Bash scripting skills through a step-by-step challenge, from variables to wildcards.

Β·

2 min read

Shell scripting is a powerful way to automate tasks and manage systems efficiently. This challenge covers essential Bash scripting concepts like comments, variables, built-in variables, and wildcards.


πŸ“Œ Understanding the Challenge

Here’s what we’ll cover in our script:

βœ… Comments (#) – Add explanatory notes in the script.
βœ… Echo (echo) – Display messages in the terminal.
βœ… Variables (var=value) – Store and manage data.
βœ… Using Variables ($((expression))) – Perform operations using stored values.
βœ… Built-in Variables ($USER, $HOME, $PWD, $SHELL, $HOSTNAME, $OSTYPE) – Access system information.
βœ… Wildcards (*.txt, ?file.*, [abc]*.sh) – List files with a specific pattern.


πŸ“ The Complete Shell Script

#!/bin/bash

# ====Task 1: Comments ====
# This script demonstrates various shell scripting concepts.

# ====Task 2: Echo ====
echo "Welcome to the Shell Scripting Challenge!"

# ====Task 3: Variables ====
name="Bash Scripting"
age=10

# Displaying variable values
echo "Topic: $name"
echo "Years of Experience: $age"

# ====Task 4: Using Variables (Simple Addition) ====
num1=10
num2=20
sum=$((num1 + num2))

echo "The sum of $num1 and $num2 is $sum"

# ====Task 5: Using Built-in Variables ====
echo "Current User: $USER"
echo "Home Directory: $HOME"
echo "Current Working Directory: $PWD"
echo "Current Shell: $SHELL"
echo "Hostname: $HOSTNAME"
echo "Operating System Type: $OSTYPE"
echo "Current Date and Time: $(date)"
echo "System Uptime: $(uptime -p)"
echo "Current Logged-in Users: $(who)"

# ====Task 6: Wildcards (Listing files based on patterns)====
echo "Listing all .txt files in the current directory:"
ls *.txt  # Lists all text files

echo "Listing all .sh files that start with 'script':"
ls script*.sh  # Lists all shell scripts starting with "script"

echo "Listing all files that start with any letter a, b, or c and end with .sh:"
ls [abc]*.sh  # Lists all shell scripts starting with a, b, or c

echo "Listing all files with any single character before 'file' and any extension:"
ls ?file.*  # Lists files like 'afile.txt', 'bfile.csv'

πŸ” Breaking Down the Script

πŸ“Œ Shebang (#!/bin/bash) – Defines the script interpreter.
πŸ“Œ Comments (#) – Used to add explanations.
πŸ“Œ Echo (echo "text") – Prints messages to the terminal.
πŸ“Œ Variables (var=value) – Stores data.
πŸ“Œ Arithmetic Operations ($((expression))) – Performs calculations.
πŸ“Œ Built-in Variables ($USER, $HOME, $PWD, $SHELL, $HOSTNAME, $OSTYPE) – Displays system info.
πŸ“Œ Wildcards (*.txt, script*.sh, [abc]*.sh, ?file.*) – Lists specific files.


πŸ›  How to Run the Script

1️⃣ Open a terminal and navigate to the script location.
2️⃣ Make the script executable:

chmod +x script.sh

3️⃣ Run the script:

./script.sh
Β