Shell Scripting

Shell Scripting

The beginning of automation!

Today In this article I am going to about Shell Script. Firstly we will start with its introduction and then we will proceed on what are the different functions which we can perform using a shell script. I will discuss echo command, read command and if-else iterations and loops. Lastly I will tell how to create a tar file using a shell script.

Introduction

Shell Scripting is a way of automating my commands and communication with the operating system using a command line tool.

There are several kinds of shells with their features. Here are the most of them in order of their release:

  1. Bourne Shell (sh)

  2. C Shell (csh)

  3. Korn Shell (ksh)

  4. Bash (Bourne Again SHell)TENEX C Shell (tcsh)

  5. Z Shell (zsh)Almquist Shell (ash)

  6. Fish (Friendly Interactive SHell)

  7. PowerShell

Shebang #!

It is used at the start of the script that specifies the path to the interpreter that should be used to execute the script.

Example:

#Bash script shebang is like
#!/bin/bash

echo and read command

The echo Command is typically used to print something on the screen. Imagine you want to print "Hello World" so you can use the echo Command to do so:

echo "Hello World"

The read command is used to take input from the user.

read input

argument

An argument is said to be a piece of value or data that one inputs from the user when the script is executed. It can be used in different operations in a script as an input.

#!/bin/bash

name=$1
echo "Hello, $name! Welcome to the script."

variables

  • Variables are symbolic names used to store data within a script.

  • They are created and assigned values using the syntax variable_name=value.

  • Variables are defined and used within the script itself.

name="John"
age=25
echo "Hello, $name! You are $age years old."

if-else condition

If a condition from a word one can guess what it will do so let's directly jump to its syntax.

if condition
then
    # code block executed if condition is true
else
    # code block executed if condition is false (optional)
fi #this is how if ends

There are various numeric comparisons that we do while writing conditions. So let's discuss them:

if [ "$num1" -eq "$num2" ]; then    # Equal to
if [ "$num1" -ne "$num2" ]; then    # Not equal to
if [ "$num1" -lt "$num2" ]; then    # Less than
if [ "$num1" -le "$num2" ]; then    # Less than or equal to
if [ "$num1" -gt "$num2" ]; then    # Greater than
if [ "$num1" -ge "$num2" ]; then    # Greater than or equal to

Same as let's dicuss about String comparisons:

if [ "$str1" = "$str2" ]; then      # Equal to
if [ "$str1" != "$str2" ]; then     # Not equal to
if [ "$str1" \< "$str2" ]; then     # Less than (in ASCII order)
if [ "$str1" \> "$str2" ]; then     # Greater than (in ASCII order)
if [[ "$str1" == "$str2" ]]; then  # Advanced string comparison (supports pattern matching)

# to check if string is empty: use the -z operator.  
if [ -z "$string" ]; then
    # Code to be executed if the string is empty
else
    # Code to be executed if the string is not empty
fi
# to check is string is not empty: use the -n operator. 
if [ -n "$string" ]; then
    # Code to be executed if the string is not empty
else
    # Code to be executed if the string is empty
fi

for loops

for variable in list
do
    # Code block to be executed for each item in the list
done

                        #OR
for ((i=1;i<n;i++))
do
    # Code block to be executed for each item in the list
done

Creating a *.tar file

To create a tar file we use the tar command

tar -cvf archive_name.tar.gz files_or_directories_to_archive
# c: it creates new archive
# v: show the progess of archiving
# f: specifies the file name
# more of them you can see using 'man tar' command

while loop

while [condition]
do
    # Code to be executed repeatedly as long as the condition is true
done