Shell_OSL_Lab
Shell_OSL_Lab
03/14/25 1
07/09/23
03/14/25 Asst. Prof.Rachna R.Chhajed 2
Bourne Shell (sh): It’s the default UNIX shell. Most of the scripts to configure
OS is written using this shell. It was developed by Stephen Bourne.
C Shell (csh): It is called C shell because the syntax used here is similar to c
language. It adds many features compare to bourne shell. This shell is not widely
used now. It was developed by Bill Joy.
Shell (ksh): This shell is backward compatible with bourne shell & inherits many
features of C shell. This was developed by David Korn.
Bash Shell (bash): It stands for Bourne again Shell i.e. It is superset of bourne
shell. It was built by Stephen Bourne.
07/09/23
03/14/25 3
Finding shell?
How to find which shell we are working at?
echo $SHELL
(or)
echo $
07/09/23
03/14/25 4
Filter Commands
07/09/23
03/14/25 5
CONCATENATING FILES:
UNIX provides a powerful utility to concatenate commands.
It is known as the catenate command, or cat for short.
It combines one or more files by appending them in the order they are listed in the
command. The input can come from the keyboard; the output goes to the monitor.
07/09/23
03/14/25 6
SHELL SCRIPT/PROGRAM
07/09/23
03/14/25 7
Practical examples where shell scripting
actively used:
07/09/23
03/14/25 8
07/09/23
03/14/25 9
#!/bin/sh
#!/bin/sh
07/09/23
03/14/25 10
07/09/23
03/14/25 11
07/09/23
03/14/25 12
How to run the shell script..
07/09/23
03/14/25 13
Variable
Variable
User Defined
System Variable
Variable
07/09/23
03/14/25 14
07/09/23
03/14/25 15
User defined variable
07/09/23
03/14/25 16
read: Making Scripts
Interactive
07/09/23
03/14/25 17
example
07/09/23
03/14/25 18
How to read array
07/09/23
03/14/25 19
Variable
Shell script output
• $ chmod =x test.sh
• $ PICT IT
07/09/23
03/14/25 20
Script Output
$ chmod u+x test.sh
$ ./test.sh
07/09/23
03/14/25 21
Shell Parameter
07/09/23
03/14/25 Asst. Prof.Rachna R.Chhajed 22
Example
07/09/23
03/14/25 Asst. Prof.Rachna R.Chhajed 23
expr : Computation and String
Handling
The shell relies on external expr command for computing
features.
Functions of expr:
•Perform arithmetic operations on integers
•Manipulate strings
Computation
expr can perform four basic arithmetic operation as well as
modulus function
07/09/23
03/14/25 24
Examples
$x=3; y=5
$expr 3 + 5
$expr $x - $y
$expr 3 \* 5
$expr 3 / 5
$expr $y % $x
Command substitution:
z =`expr $x + $y`
Incrementing value of a
variable
x =`expr $x + 1`
07/09/23
03/14/25 25
Small script which you can do…
1.Define variable num with value 10 and print it on screen.
07/09/23
03/14/25 26
THE if CONDITIONAL
if command is if command is if command is
successful successful successful
then then then
execute command execute command execute command
else fi elif command is
execute command successful
fi then ….
else …..
fi
07/09/23
03/14/25 27
if...else…fi -Example
Try it as follows:
$ chmod 755 isnump_n
$ isnump_n 5
5 number is positive
$ isnump_n -45
-45 number is negative
$ isnump_n
./ispos_n : You must
give/supply one integers
$ isnump_n 0
0 number is negative
03/14/25 28
Mathematical operator..
Normal
Mathemat Arithmetic
ical al/
Operator Meaning Mathemat But in Shell
in Shell ical
Script Statement For test For [ expr ]
s statement statement
with if with if
command command
if test 5-
-eq is equal to 5 == 6 if [ 5 -eq 6 ]
eq 6
if test 5-
-ne is not equal to 5 != 6 if [ 5 -ne 6 ]
ne 6
if test 5 -lt
-lt is less than 5<6 if [ 5 -lt 6 ]
6
is less than or if test 5 -le
-le 5 <= 6 if [ 5 -le 6 ]
equal to 6
if test 5 -gt
-gt is greater than 5>6 if [ 5 -gt 6 ]
6
is greater than or if test 5-
-ge 5 >= 6 if [ 5 -ge 6 ]
equal to ge 6
07/09/23
03/14/25 29
for and while loop
• Output
07/09/23
03/14/25 30
07/09/23
03/14/25 31
For loop with unix commands
07/09/23
03/14/25 32
while loop
07/09/23
03/14/25 33
07/09/23
03/14/25 34
# ` done
While loop
#!/bin/sh
a=0
while [ "$a" -lt 10 ]
do
b ="$a“
while [ "$b" -ge 0 ]
do
echo -n "$b " b=`expr $b
b=`expr $b - 1`
done
echo a=`expr $a + 1`
done
07/09/23
03/14/25 35
-1
Few example of while script..
07/09/23
03/14/25 36
Read file using while loop
07/09/23
03/14/25 37
Until loop
07/09/23
03/14/25 38
Case statement
07/09/23
03/14/25 39
07/09/23
03/14/25 40
Select …in comaand
07/09/23
03/14/25 41
Practice Assignments
07/09/23
03/14/25 42
Reference books available in library
07/09/23
03/14/25 43