Day 1 of the Bash Scripting Challenge! ๐Ÿš€

Day 1 of the Bash Scripting Challenge! ๐Ÿš€

ยท

4 min read

Introduction

Bash scripting is a powerful and versatile skill that allows you to automate tasks and perform complex operations using the command-line interface of a Unix-based operating system, such as Linux or macOS. Bash which stands for "Bourne Again Shell" is the default shell for many Unix-like systems and serves as a command interpreter for executing commands and scripts.

Task 1: Comments

In bash scripts, comments are the non-executed part of the script. They provide useful information or explanations about the code. They play an important role in making the script more understandable not only for the author but also for the developers who might need to work with the script.

  1. Single-line comments: Single-line comments begin with (#) and extend till the end of the line. A line beginning with the # symbol will be completely ignored by the compiler. For example:
#!/bin/bash

# Task 1: Comments
# This is a single-line comment
echo "Hello World!"
  1. Multi-line comments: There are multiple ways through which you can mention multiple comments in a bash script. One such method is to use (:'). For example:
#!/bin/bash
: '
This is a multi-line comment block.
You can add as many lines as you want.
The code below will not be executed:

echo "This line will not be executed."
'
echo "Hello World!"

Task 2: Echo

In bash, an echo command is a command used to output or display messages on the terminal. It is one of the most commonly used commands in bash script.

#!/bin/bash

# Task 2: Echo
# In this task, we will use the 'echo' to display a message.
echo "Hello World!"

Task 3: Variables

In bash, variables are used to store data and values that can be referenced and manipulated throughout the script. They are a fundamental part of shell scripting and allow you to store temporary or permanent data. In this task, we will create a bash script that declares variables and assigns value to them.

#!/bin/bash

# Task 3: Variables
# Remember to avaoid space between the '=' and variable
name="Sutish Pradhan"
echo "Hello, $name How are you today?"

Task 4: Using Variables

In the previous task, we declared the variables. In this task, we will now make use of them to perform the given task. So we'll simply create two variables of type number and then print their sum.

#!/bin/bash

# Task 4: Using Variables
# In this task we will perform the addition of two numbers 

# Take the input for the variables from the user
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2

# Calculate the sum of two numbers
result=$((num1 + num2))

# Display the result
echo "The sum of two given numbers is: $result"

Task 5: Using Built-in Variables

Built-in variables are the special variables whose values are defined and maintained by the bash or the operating system. These variables are predefined and have specific names that are recognized by the shell. In this task, we will create a bash script that utilizes different built-in variables.

#!/bin/bash

# Task 5: Built-in Variables
# In this task we will be utilizing built-in variables

# $HOME represents the home directory of the current user
echo "Your home directory is $HOME"

# $USER holds the username of the current user
echo "You are logged in as $USER"

# $PWD represents the present working directory
echo "current working directory is $PWD"

# $HOSTNAME represents the current name of the host/machine
echo "Your Host name is $HOSTNAME"

# $OSTYPE defines the operating system type 
echo "Operating system type $OSTYPE"

Task 6: Wildcards

Wildcards are characters that allow you to perform pattern matching on files and directories in a shell script. Wildcards are handy for working with multiple files that share a common pattern

#!/bin/bash

# Task 6: Wildcards
# In this task, we will use wildcards to list files 

# Define the target directory
for file in *.txt; do

# List all files with a specific extension in the target directory
echo "Processing $file"
done

Thank you for reading!

Contact me at :

Linkedin: https://www.linkedin.com/in/sutish-pradhan/

E-mail:

ย