๐Ÿš€ Discovering the Sorcery๐Ÿง™โ€โ™‚๏ธ of Shell Scripting in Linux! ๐Ÿง

ยท

5 min read

๐Ÿš€ Discovering the Sorcery๐Ÿง™โ€โ™‚๏ธ of Shell Scripting in Linux! ๐Ÿง

Introduction ๐ŸŒŸ

Shell plays a very important role in Linux architecture. The shell sits between the application and Kernal. it acts like a command interpreter. it reads your terminal input and translates the commands into actions taken by the system.

Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of the shell has its own set of recognized commands and functions.

Types Of Shells๐Ÿš

In Linux, there are two major types of shells โˆ’

  • Bourne shell โˆ’ If you are using a Bourne-type shell, the $ character is the default prompt.

  • C shell โˆ’ If you are using a C-type shell, the % character is the default prompt.

The Bourne Shell has the following subcategories โˆ’

  • Bourne shell (sh)

  • Korn shell (ksh)

  • Bourne Again shell (bash)

  • POSIX shell (sh)

The different C-type shells follow โˆ’

  • C shell (csh)

  • TENEX/TOPS C shell (tcsh)

The original Unix shell was written in the mid-1970s by Stephen R. Bourne while he was at the AT&T Bell Labs in New Jersey.

Bourne shell was the first shell to appear on Unix systems, thus it is referred to as "the shell".

Bourne shell is usually installed as /bin/sh on most versions of Unix. For this reason, it is the shell of choice for writing scripts that can be used on different versions of Unix.

In this chapter, we are going to cover most of the Shell concepts that are based on the Borne Shell.

Example of Shell Scripting

Assume we create a test.sh file. Note all the scripts file would have the .sh extension. Shell files should have executable permission. with executable permission, we can run our shell file. Before you add anything else to your script, you need to alert the system that a shell script is being started. This is done using the shebang construct. For example โˆ’

#!/bin/sh

This tells the system that the commands that follow are to be executed by the Bourne shell. It's called a shebang because the # symbol is called a hash, and the ! symbol is called a bang.

To create a script containing these commands, you put the shebang line first and then add the commands โˆ’

#!/bin/bash
pwd
ls

Types of Shell Scripting

Basic Shell Scripting

Output to screen

#!/bin/bash
# Simple output script
echo "Hello World"

Defining Tasks

#!/bin/bash
# Define small tasks
whoami
echo
pwd
echo
hostname
echo
ls -ltr
echo

Defining variables

#!/bin/bash
# Example of defining variables
name=Ram
surname=Shinde
topic=Shell Scripting
echo "My first name is $name"
echo "My surname is $surname"
echo "Currently I am learning $topic"

Read Input

#!/bin/bash
# Read user input
echo "What is your first name?"
read a
echo
echo "What is your last name?"
read b
echo
echo Hello $a $b

Scripts to run commands within

#!/bin/bash
# Script to run commands within
clear
echo "Hello `whoami`"
echo
echo "Today is `date`"
echo
echo "Number of user login: `who | wc -l`"
echo

in the above script, backtick is used for commands in printing lines. it's not a single quote. you can see the backtick easily on the left corner under the Esc.

Read input and perform a task

#!/bin/bash
# This script will rename a file
echo Enter the file name to be renamed
read oldfilename
echo Enter the new file name
read newfilename
mv $oldfilename $newfilename
echo The file has been renamed as $newfilename

For loop Script

Simple for loop output

#!/bin/bash
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done

for loop to create 5 files named 1-5

#!/bin/bash
for i in {1..5}
do
touch $i
done

Specify days in for loop

#!/bin/bash
i=1
for day in Mon Tue Wed Thu Fri
do
echo "Weekday $((i++)) : $day"
done

List all users one by one from /etc/passwd file

#!/bin/bash
i=1
for username in `awk -F: '{print $1}' /etc/passwd`
do
echo "Username $((i++)) : $username"
done

Do-while Script

Script to run for a number of times

#!/bin/bash
c=1
while [ $c -le 5 ]
do
echo "Welcone $c times"
(( c++ ))
done

Script to run for a number of seconds

#!/bin/bash
count=0
num=10
while [ $count -lt 10 ]
do
echo
echo $num seconds left to stop this process $1
echo
sleep 1
num=`expr $num - 1`
count=`expr $count + 1`
done
echo
echo $1 process is stopped!!!
echo

if-then Script

Check the variable

#!/bin/bash
count=100
if [ $count -eq 100 ]
then
echo Count is 100
else
echo Count is not 100
fi

Check if a file error.txt exist

#!/bin/bash
clear
if [ -e /home/iafzal/error.txt ]
then
echo "File exist"
else
echo "File does not exist"
fi

Check the response and then output

#!/bin/bash
clear
echo
echo "What is your name?"
echo
read a
echo
echo Hello $a sir
echo
echo "Do you like working in IT? (y/n)"
read Like
echo
if [ "$Like" == y ]
then
echo You are cool
elif [ "$Like" == n ]
then
echo You should try IT, itโ€™s a good field
echo
fi

Case Script

#!/bin/bash
echo
echo Please chose one of the options below
echo
echo 'a = Display Date and Time'
echo 'b = List file and directories'
echo 'c = List users logged in'
echo 'd = Check System uptime'
echo
read choices
case $choices in
a) date;;
b) ls;;
c) who;;
d) uptime;;
*) echo Invalid choice - Bye.
esac

I hope that this script is very useful to all who have an interest in shell scripting.

Keep Practising.

And if anyone has any doubt please drop a comment I will try to solve your doubts.

Thank You!

ย