Unix - Lab Manual 02
Unix - Lab Manual 02
mkdir , cd , rmdir , rm :
cp :
ls , cat :
chmod :
vi editor :
#!/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
Output :
#!/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"
Output :
#!/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)
#!/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)
#!/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)
#!/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)
#!/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
do
do
echo -n "*"
done
Output :
Department of CSE 16 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
#!/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)
#!/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
Output :
Department of CSE 20 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
Output:
Department of CSE 21 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
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)
#!/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)
Output :
Department of CSE 25 | P a g e
Unix and Shell Programming Lab (CSE-6217L)
Department of CSE 26 | P a g e