Shell Scripting
DR. SHWETA SHARMA
ASSISTANT PROFESSOR
DEPT. OF COMPUTER ENGINEERING
NIT KURUKSHETRA
Basic Terminologies
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 2
User Mode and Kernel Mode
User Mode: When the system is executing on behalf of the user
Kernel Mode: Whenever the operating system takes over the control of the
computer system. The hardware switches from user mode to kernel mode when
an interrupt occurs
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 3
Interrupts
Caused by hardware (pressing a key) or software (dividing by zero exception)
Events that take place to inform the OS to stop the current execution of the current
process
CPU transfers control to interrupt service routine (ISR) to handle the interrupt
ISR finds out which software or hardware caused the interrupt
Service the request
Resumes the execution of the previous process
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 4
Shell Scripting
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 5
1. Uses for Scripting Languages
Web Development: JavaScript, for example, is a key scripting language for creating
dynamic and interactive web pages (Other languages: PHP, Python, and Ruby)
Automation: For automating repetitive tasks
System Administration: Shell scripting languages like Bash and PowerShell are commonly
used to facilitate the automation of routine tasks
Data Processing and Analysis: Python
Network Programming: Python to manage network devices
GUI Development: Python with Tkinter, JavaScript
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 6
2. Shell
A shell is a special user program that provides an interface for the user to use Operating
System services
Shell accepts human-readable commands from users and converts them into something
which the kernel can understand
Command-line interpreter executes commands read from input devices such as keyboards
or from files
The shell gets started when the user logs in or starts the terminal
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 7
3. Kernel
• Core part that interacts directly with the hardware
• Interface between the user applications and the hardware
• Interrupt Handling, I/O communication, Process management, Memory
management, Device drivers
• One of the first programs to be loaded up on the memory by the boot loader
• Manages the memory as well as peripherals such as keyboards and monitors
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 8
3. Kernel
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 9
4. Shell Scripts
Computer program designed to be run by the Unix shell
It is a sequence of commands written in a script file, with a .sh extension, that the shell can
execute directly
To automate repetitive tasks, perform system administration tasks, and sequence multiple
commands
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 10
Program 1- Hello World
echo "Hello World"
To run this script:
Save the script to a file: hello_world.sh
Open a terminal and navigate to the directory where the script is saved
Make the script executable by running: chmod +x hello_world.sh
Run the script: ./hello_world.sh
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 11
Program 2- To read user input
#!/bin/bash
echo "Hello! What's your name?"
read username # Read the user input from Standard Input and store in Variable username
echo "Nice to meet you, $userName!"
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 12
Program 3- To read user input and perform
operations with them
#!/bin/bash
echo -n "Enter number 1 : " # -n option prevents newline
read NUM1
echo -n "Enter number 2 : "
read NUM2
SUM=`expr $NUM1 + $NUM2`
echo "The sum is $SUM"
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 13
Program 4- if condition
#!/bin/bash
NUM1=10
NUM2=5
if [ $NUM1 -gt $NUM2 ]
then
echo "NUM1 > NUM2"
fi #to mark the end of the if statement
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 14
Program 5- if else condition
#!/bin/bash
NUM1=5
NUM2=12
if [ $NUM1 -lt $NUM2 ]
then
echo "NUM1 < NUM2"
else
echo "NUM1 > NUM2"
fi
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 15
Program 6- elif condition
#!/bin/bash
echo -n "Enter a number: "
read NUM
if [ $NUM -gt 0 ]
then
echo "$NUM is +ve"
elif [ $NUM -lt 0 ]
then
echo "$NUM is -ve"
else
echo "$NUM is 0"
fi
echo "done"
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 16
Program 7- Case
#!/bin/bash
echo -n "Enter a character: "
read CHAR
case $CHAR in
a | A) echo "You entered $CHAR which is a vowel";;
e | E) echo "You entered $CHAR which is a vowel";;
i | I) echo "You entered $CHAR which is a vowel";;
o | O) echo "You entered $CHAR which is a vowel";;
u | U) echo "You entered $CHAR which is a vowel";;
*) echo "You entered $CHAR which is not a vowel";;
esac
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 17
Program 8- operator = and !=
#!/bin/bash
STR1="Hello"
STR2="Hello"
if [ ${STR1} = ${STR2} ]
then
echo "Strings match"
else
echo "Strings don't match"
fi
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 19
Program 9- logical operators: AND OR
#!/bin/bash echo -n "Enter another NUM: "
echo -n "Enter a NUM: " read NUM
read NUM
# OR -o operator
# AND -a operator
if [ $NUM -lt 10 -o $NUM -gt 20 ]
if [ $NUM -ge 10 -a $NUM -le 20 ]
then
then
echo "$NUM is between 10 and 20"
echo "$NUM is NOT between 10 and 20"
else else
echo "$NUM is NOT between 10 and 20" echo "$NUM is between 10 and 20"
fi fi
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 20
Program 10- while
#!/bin/bash
COUNT=0
while [ $COUNT -lt 5 ]
do
echo "Loop count is ${COUNT}"
COUNT=$((COUNT + 1))
done
echo "Done"
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 21
Program 11- for
#!/bin/bash
COUNT=0
for i in 0 1 2 3 4
do
echo "Loop count is ${COUNT}"
COUNT=$((COUNT + 1))
Done
echo "Done"
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 22
Program 12- Functions
#!/bin/bash
function find_sum()
{
SUM=`expr $1 + $2`
echo $SUM
50
}
800
find_sum 20 30
RESULT=`find_sum 300 500`
echo $RESULT
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 23
Thank you!
1/15/2024 DR. SHWETA SHARMA, NIT KURUKSHETRA 24