Unix and Shell Programming Lab (CSE-6217L)
1) Execution of general-purpose utility commands
who, cal , date , time , echo:
Department of CSE 1|Page
Unix and Shell Programming Lab (CSE-6217L)
mkdir , cd , rmdir , rm :
cp :
Department of CSE 2|Page
Unix and Shell Programming Lab (CSE-6217L)
2) Commands like cat, ls, chmod and vi editor
ls , cat :
chmod :
Department of CSE 3|Page
Unix and Shell Programming Lab (CSE-6217L)
vi editor :
Department of CSE 4|Page
Unix and Shell Programming Lab (CSE-6217L)
3) Grep filter and its different pattern options :
Department of CSE 5|Page
Unix and Shell Programming Lab (CSE-6217L)
4) Simple shell script for basic Arithmetic Calculation
#!/bin/sh
a=10
b=20
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a != $b ]
then
echo "a is not equal to b"
else
echo "a is equal to b"
fi
Department of CSE 6|Page
Unix and Shell Programming Lab (CSE-6217L)
Output :
Department of CSE 7|Page
Unix and Shell Programming Lab (CSE-6217L)
5) Program to check whether a number entered prime or composite number.
#!/bin/bash
echo -n "ENTER THE NUMBER: "
read n
f=0
mid=`expr $n \/ 2`
x=2
while [ $x -le $mid ]
do
rem=`expr $n % $x`
if [ $rem -eq 0 ]
then
done
f=1
fi
x=`expr $x + 1`
if [ $f -eq 0 ]
then
echo "NUMBER IS PRIME"
else
echo "NUMBER IS COMPOSITE"
Department of CSE 8|Page
Unix and Shell Programming Lab (CSE-6217L)
Output :
Department of CSE 9|Page
Unix and Shell Programming Lab (CSE-6217L)
6) Shell Program to find factorial of number
#!/bin/bash
echo -n "ENTER THE NUMBER: "
read n
fact=1
x=2
while [ $x -le $n ]
do
fact=`expr $fact \* $x`
x=`expr $x + 1`
done
echo "FACTORIAL OF $n = $fact"
Output :
Department of CSE 10 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
7) Shell Program to generation of Fibonacci series of number
#!/bin/bash
echo -n "ENTER THE NUMBER: "
read n
f1=0
f2=1
while [ $n -ge 1 ]
do
f3=`expr $f1 + $f2`
echo "$f1"
f1=$f2
f2=$f3
n=`expr $n - 1`
done
Output :
Department of CSE 11 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
8) Program to implement IF-ELSE condition
#!/bin/bash
echo -n "ENTER THE FIRST NUMBER: "
read a
echo -n "ENTER THE SECOND NUMBER: "
read b
if [ $a -gt $b ]
then
echo "A is Greater"
else
echo "B is Greater"
fi
Output :
Department of CSE 12 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
9) Program to implement NESTED-IF-CONDITION
#!/bin/bash
echo -n "ENTER THE FIRST NUMBER: "
read a
echo -n "ENTER THE SECOND NUMBER: "
read b
echo -n "ENTER THE THIRD NUMBER: "
read c
if [ $a -gt $b ]
then
echo "A is Greater"
elif [ $b -ge $c ]
then
echo "B is Greater"
else
echo "C is Greater"
fi
Department of CSE 13 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
Output :
Department of CSE 14 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
10) Program on case statement
#!/bin/bash
echo -n "ENTER THE NUMBER: "
read n
case $n in
1)
echo "Pressed 1" ;;
2)
echo "Pressed 2" ;;
3)
echo "Pressed 3" ;;
*)
echo "Pressed other than 1 or 2 or 3"
esac
Output :
Department of CSE 15 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
11) Generate a program to display the given pattern using for loop
#!/bin/bash
for (( i=1; i<=4; i++ ))
do
for (( j=1; j<=$i; j++ ))
do
echo -n "*"
echo " "
done
Output :
Department of CSE 16 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
12) Check whether the STRING is Palindrome OR Not
#!/bin/bash
echo -n "ENTER A STRING: "
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
rev=" "
while [ $len -gt 0 ]
do
r=`echo $str | cut -c $len`
rev=$rev$r
len=`expr $len - 1`
done
echo " "
echo "ORGINAL STRING:$str"
echo "REVERSED STRING:$rev"
if [ $str = $rev ]
then
echo "Palindrome"
else
echo "Not Palindrome"
fi
Department of CSE 17 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
Output :
Department of CSE 18 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
13) Addition of Two numbers using Function
#!/bin/bash
function add()
{
x=$1
y=$2
echo "SUM=`expr $x + $y`"
}
echo "ENTER THE FIRST NUMBER: "
read one
echo "ENTER THE SECOND NUMBER: "
read two
add $one $two
Output :
Department of CSE 19 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
14) Various AWK commands/programs using different options, patterns and actions
a) awk '/shahid/ { print}' abbbb
b) awk '{print}' abbbb
c) awk '/shahid/' abbbb
d) awk -F"|" '{print $2,$4}' abbbb
Output :
Department of CSE 20 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
e) awk -F"|" '/sha[hi]*d/' abbbb
f) awk -F"|" '/sha[hi]*d|teacher/' abbbb
g) awk -F"|" '/sha[hi]*d|teacher/ {print $2,$3}' abbbb
h) awk -F"|" '{printf "%-15d %-15s\n",$1*10,$2}' abbb
Output:
Department of CSE 21 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
15) Execution of AWK using files.
BEGIN {
printf "Serial\tEmployeeID\tName\n"
}
{a=a +1; printf "%-10d %-10d %s\n",a,$1,$2}
END {
printf "\n\t total results= %d",a
}
To execute above file use following command:
awk -F"|" -f files.awk abbbb
Output :
Department of CSE 22 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
16) Shell program to find LCM of two numbers
#!/bin/bash
echo "enter first number"
read a
echo "enter second number"
read b
p=`expr $a \* $b`
while [ $b -ne 0 ]
do
r=`expr $a % $b`
a=$b
b=$r
done
LCM=`expr $p / $a`
echo "LCM=$LCM"
Output :
Department of CSE 23 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
18) Shell program to create student database along with various options for
manipulating the data.
i="y"
echo "Enter name of database "
read db
while [ $i = "y" ]
do
clear
echo "1.View the Data Base "
echo "2.View Specific Records "
echo "3.Add Records "
echo "4.Delete Records "
echo "5.Exit "
echo "Enter your choice "
read ch
case $ch in
1)cat $db;;
2)echo "Enter id "
read id
grep -i "$id" $db;;
3)echo "Enter new std id "
read nid
echo "Enter new name:"
read nm
echo "Enter designation "
read des
echo "Enter college name"
read college
echo "$nid $nm $des $college">>$db;;
Department of CSE 24 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
4)echo "Enter Id"
read id
# set -a
# sed '/$id/d' $db>dbs1
grep -v "$id" $db>dbs1
echo "Record is deleted"
cat dbs1;;
5)exit;;
*)echo "Invalid choice ";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done
Output :
Department of CSE 25 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
Department of CSE 26 | P a g e