You are on page 1of 23

OS lab

Week-2
Shell
Introduction to Linux Shell and Shell
Scripting
• If you are using any major operating system you are indirectly interacting
to shell.
• If you are running Ubuntu, Linux Mint or any other Linux distribution, you
are interacting to shell every time you use terminal.
• so before understanding shell scripting we have to get familiar with following
terminologies

• Kernel
• Shell
What is kernel?

• The kernel is a computer program that is the core of a computer’s


operating system, with complete control over everything in the
system. It manages following resources of the Linux system –
• File management
• Process management
• I/O management
• Memory management
• Device management etc.
What is Shell?
• A shell is special user program which provide an interface to user to
use operating system services.
• Shell accept human readable commands from user and convert them
into something which kernel can understand.
• It is a command language interpreter that execute commands read
from input devices such as keyboards or from files.
• The shell gets started when the user logs in or start the terminal.
Fig: Linux shell
• A Kernel makes the communication between the hardware and
software possible. While the Kernel is the innermost part of an
operating system, a shell is the outermost one.

• A shell in a Linux operating system takes input from you in the form of
commands, processes it, and then gives an output. It is the interface
through which a user works on the programs, commands, and scripts.
A shell is accessed by a terminal which runs it.

• When you run the terminal, the Shell issues a command


prompt, where you can type your input, which is then executed when
you hit the Enter key. The output or the result is thereafter displayed
on the terminal.
• Shell is broadly classified into two categories –

• Command Line Shell


• Graphical shell
• There are several shells are available for Linux systems like –

• BASH (Bourne Again SHell) – It is most widely used shell in Linux systems. It is
used as default login shell in Linux systems and in macOS. It can also be
installed on Windows OS.
• CSH (C SHell) – The C shell’s syntax and usage are very similar to the C
programming language.
• KSH (Korn SHell) – The Korn Shell also the base for the POSIX Shell standard
specifications etc.

• Each shell does the same job but understand different commands and
provide different built in functions.
Shell script
A shell script have syntax just like any other programming language. If
you have any prior experience with any programming language like
Python, C/C++ etc. it would be very easy to get started with it.

A shell script comprises following elements –


• Shell Keywords – if, else, break etc.
• Shell commands – cd, ls, echo, pwd etc.
• Functions
• Control flow – if..then..else, case and shell loops etc.
Ex:1 welcome
• $vi first.sh

• # My first shell script


• echo "first shell script“

#It will give execution permission


• $chmod +x first.sh

# for execute the shell script


$./first.sh
[Or]

• Sh file1.sh
Ex2: Print details of a person

• echo "what is your name?"


• read name

• echo "How do you do, $name?"


• read remark

• echo "I am $remark too!"


Ex:3 Adding two values
• # adding two values

• ((sum = 25 +35 ))

• #print the result


• Echo $sum
Ex 4: the square of a number
• ‘ this script calculates // multi-line comments
The square of 5.’

• ((area=5*5))

• Echo $area
Ex 5: The While loop
• i=0
• while [ $i -le 2 ] //i less than equal to 2
• do
• echo Number: $i
• ((i++))
• done
Ex 6: The for loop

• for (( counter=1; counter<=10; counter++ ))


do
echo -n "$counter "
done
printf "\n"
Ex:7 The If statement
echo -n "Enter a number: "

read num

# if num is greater than 10

if [[ $num -gt 10 ]]
then
echo "Number is greater than 10."
fi
Some of the commands:

• -gt greater than

• -lt Less than

• -le less than equal

• -ge Greater than equal


2.a)Write a Shell script that accepts a filename, starting and ending line numbers as arguments and displays all the
lines between the given line numbers 

• echo “enter a file name”


• read fname
• echo “enter starting line number”
• read sl
• echo “enter ending line number”
• read el
• d=`expr $el - $sl`
• if [ -f $fname ]
• then
• echo “ the line between $sl and $el line numbers are”
• head -$el $fname | tail -$d
• else
• echo “file does not exist”
• fi

 
INPUT:

• student@ubuntu:~$ gedit prog1.sh


• student@ubuntu:~$ sh prog1.sh
• enter the filename
file1.txt
• enter the starting line number
2
• enter the ending line number
6
2b). Write a shell script that deletes all lines containing a specified word in one or more files supplied as arguments to
it. 

• if [ $# -ne 0 ]
• then
• echo “enter the word”
• read word
• for fname in $*
• do
• if [ -f $fname ]
• then
• echo “ the given input file name is $fname”
• grep –v “$word” $fname
• else
• echo “ $fname is a not a file”
• fi
• done
• else
• echo “enter at least one argument as input”
• fi
• INPUT:
• ## I am creating two files names are del , dell.
• Gcet@ubuntu:~$ gedit del
• unix is os
• dos is also os
• here using unix
• unix is powerful os
• ---------------------------
• Gcet@ubuntu:~$ gedit dell
• windowsnt is also os
• there are some difference between unix and windows
• but unix is great among all os
• OUTPUT:
• Gcet@ubuntu:~$ sh Prog2.sh del dell
• enter the word
• os
• the given input file name is del
• here using unix
• 
• the given input file name is dell
• there are some difference between unix and windows

You might also like