Programs 1
Programs 1
read opt
case $opt in
1)
echo "Enter filename"
read fileName
if [ -e $fileName ] ; then # -e to check if file exists, if exits remove
the file
rm $fileName
fi
cont=1
echo
"NAME\tNUMBER\t\tADDRESS\n===============================\n" |
cat >> $fileName
while [ "$cont" -gt 0 ]
do
echo "\nEnter Name"
read name
echo "Enter Phone Number of $name"
read number
echo "Enter Address of $name"
read address
echo "$name\t$number\t\t$address" | cat >> $fileName
echo "Enter 0 to Stop, 1 to Enter next"
read cont
done
;;
2)
cat $fileName
;;
3)
echo "\nEnter Name"
read name
echo "Enter Phone Number of $name"
read number
echo "Enter Address of $name"
read address
echo "$name\t$number\t\t$address" | cat >> $fileName
;;
4)
echo "Delete record\nEnter Name/Phone Number"
read pattern
temp="temp"
grep -v $pattern $fileName | cat >> $temp
rm $fileName
cat $temp | cat >> $fileName
rm $temp
;;
5)
echo "Modify record\nEnter Name/Phone Number"
read pattern
temp="temp"
grep -v $pattern $fileName | cat >> $temp
rm $fileName
cat $temp | cat >> $fileName
rm $temp
echo "Enter Name"
read name
echo "Enter Phone Number of $name"
read number
echo "Enter Address of $name"
read address
echo -e "$name\t$number\t$address" | cat >> $fileName
;;
esac
done
Output:
Choose one of the Following
1. Create a New Address Book
2. View Records
3. Insert new Record
4. Delete a Record
5. Modify a Record
6. Exit
1
Enter filename
add.txt
\nEnter Name
rupam
Enter Phone Number of rupam
97277244456
Enter Address of rupam
alkapuri
Enter 0 to Stop, 1 to Enter next
1
\nEnter Name
ddd
Enter Phone Number of ddd
890765
Enter Address of ddd
vadodara
Enter 0 to Stop, 1 to Enter next
1
\nEnter Name
yuio
Enter Phone Number of yuio
67890
Enter Address of yuio
jarod
Enter 0 to Stop, 1 to Enter next
2
\nEnter Name
ghjk
Enter Phone Number of ghjk
45678
Enter Address of ghjk
anand
Enter 0 to Stop, 1 to Enter next
0
Choose one of the Following
1. Create a New Address Book
2. View Records
3. Insert new Record
4. Delete a Record
5. Modify a Record
6. Exit
2
NAME\tNUMBER\t\tADDRESS\n===============================\n
rupam\t97277244456\t\talkapuri
ddd\t890765\t\tvadodara
yuio\t67890\t\tjarod
ghjk\t45678\t\tanand
Choose one of the Following
1. Create a New Address Book
2. View Records
3. Insert new Record
4. Delete a Record
5. Modify a Record
6. Exit
5
Modify record\nEnter Name/Phone Number
rupam
Enter Name
rupam gupta
Enter Phone Number of rupam gupta
9727724441
Enter Address of rupam gupta
alkapuri
Choose one of the Following
1. Create a New Address Book
2. View Records
3. Insert new Record
4. Delete a Record
5. Modify a Record
6. Exit
2
NAME\tNUMBER\t\tADDRESS\n===============================\n
ddd\t890765\t\tvadodara
yuio\t67890\t\tjarod
ghjk\t45678\t\tanand
rupam gupta 9727724441 alkapuri
Choose one of the Following
1. Create a New Address Book
2. View Records
3. Insert new Record
4. Delete a Record
5. Modify a Record
6. Exit
2. Write a shell script to generate marksheet of a student. Take 3 subjects, calculate and
display total marks, percentage and Class obtained by the student.
echo "Enter marks(out of 100) of "
read -p "Subject 1: " s1
read -p "Subject 2: " s2
read -p "Subject 3: " s3
sum=`expr $s1 + $s2 + $s3`
echo "Sum of marks of 3 subjects is : "$sum
per=` expr $sum \* 100 / 300 `
echo "Percentage: "$per
if [ $per -ge 70 ]
then
echo "you got Distinction"
elif [ $per -ge 60 ]
then
echo "you got First class"
elif [ $per -ge 50 ]
then
echo "you got Second class"
elif [ $per -ge 40 ]
then
echo "You got Pass class"
else
echo "sorry you failed! better luck next time"
fi
Multiplication Table:
Which table do you want ?
5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
read n
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
How many number of terms to be generated ?
15
Fibonacci Series up to 15 terms :
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
6. Shell script will check entered string is palindrome or not.
echo "Enter the string:"
read str
echo
len=`expr $len - 1`
i=1
j=`expr $len / 2`
if test $k != $l
then
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is palindrome"
Enter the string:
awwwa
String is palindrome
7. Write a shell script to read n numbers as command arguments and sort them in
descending order.
if [ $# -eq 0 ]
then
echo "Enter valid Arguments"
exit
fi
> temp
for i in $*
do
echo "$i" >> temp
done