Linux - Day 8

Linux - Day 8

Shell Scripting Challenge on Hashnode

Introduction

Shell scripting might sound technical, but it's actually a handy tool to automate repetitive tasks on your computer. In today's challenge, we'll explore some basic yet powerful concepts in bash scripting. This is Day 8 of the Shell Scripting Challenge, and we’ll cover comments, echo, variables, built-in variables, and wildcards.

By the end of this post, you’ll know how to write simple bash scripts and even complete a full project! Let’s jump in.


Task 1: Comments in Bash Scripts

Goal: Use comments to explain what the script does.

Comments are text notes in your script that don’t run—they're just there to explain things. In bash scripts, comments start with a # symbol. Here’s how you can use them:

#!/bin/bash
# This is a comment. It explains what the script does.
# The next line prints a message to the terminal.
echo "Hello, welcome to Day 8 of the Shell Scripting Challenge!"

What’s happening here:

  • #!/bin/bash tells your system to run the script using bash.

  • The # makes sure the comments are ignored by bash and don’t affect your script. It’s just for human readers.


Task 2: Echo in Bash

Goal: Use the echo command to display a message in the terminal.

echo is one of the easiest bash commands—it simply prints whatever you tell it to. Let’s see it in action:

#!/bin/bash
# Printing a message with echo
echo "This is my first bash script for the Day 8 challenge!"

What’s happening here:

  • echo is like saying "print this" in bash. It shows whatever text you write in the terminal window when you run the script.

Task 3: Variables in Bash

Goal: Declare variables and assign values to them.

A variable is like a container that holds data. Here’s how you can create and use variables in bash:

#!/bin/bash
# Declaring variables
name="Hashnode"
task="Shell Scripting Challenge"

# Using echo to display the value of variables
echo "Welcome to the $name $task!"

What’s happening here:

  • We created two variables: name and task.

  • To use a variable, add $ before the variable name. Here, $name and $task hold the values we assigned earlier.


Task 4: Using Variables for Arithmetic

Goal: Use variables to add two numbers.

Variables can hold numbers too! Let's use two variables to store numbers and calculate their sum:

#!/bin/bash
# Declaring two numbers
num1=5
num2=10

# Adding the numbers
sum=$((num1 + num2))

# Printing the result
echo "The sum of $num1 and $num2 is: $sum"

What’s happening here:

  • $((...)) tells bash to do arithmetic inside the parentheses.

  • We store the sum in the variable sum and then print it out.


Task 5: Using Built-in Variables

Goal: Use built-in variables to show system info.

Bash comes with some built-in variables that store useful system information. Here’s how we can access them:

#!/bin/bash
# Displaying built-in variable values
echo "Current User: $USER"
echo "Home Directory: $HOME"
echo "Current Working Directory: $PWD"

What’s happening here:

  • $USER: Shows the current logged-in user.

  • $HOME: Shows the path to your home directory.

  • $PWD: Shows the current directory you're working in.


Task 6: Using Wildcards

Goal: Use wildcards to list files with a specific extension.

Wildcards let you work with patterns, like file names. For example, let’s list all .txt files in the current directory:

#!/bin/bash
# Listing all .txt files in the current directory
echo "List of .txt files:"
ls *.txt

What’s happening here:

  • The * wildcard matches any number of characters. So *.txt matches all files that end with .txt.

  • ls lists the files that match the pattern.


Complete Script

Here’s the full bash script that includes all tasks from above:

#!/bin/bash
# Task 1: Comments
# This script covers Day 8 tasks for the Shell Scripting Challenge.

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

# Task 3: Variables
name="Hashnode"
challenge="Shell Scripting"
echo "You are participating in the $challenge on $name!"

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

# Task 5: Built-in Variables
echo "User: $USER"
echo "Home Directory: $HOME"
echo "Current Directory: $PWD"

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

Conclusion

Today, we covered how to write bash scripts with comments, variables, and built-in tools like wildcards. These are just the basics, but they are super useful when automating tasks or handling repetitive work. I hope you found this tutorial helpful. Good luck with the Shell Scripting Challenge!

#bash #shellscripting #linux #programming