0% found this document useful (0 votes)
14 views9 pages

Programs 1

The document contains multiple shell script examples that implement various functionalities including an address book, marksheet generation, multiplication table, prime number display, Fibonacci series generation, palindrome check, and sorting numbers. Each script is designed to interact with the user through prompts and provides specific outputs based on user input. The examples demonstrate basic shell scripting techniques and control structures.

Uploaded by

Shobhit Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

Programs 1

The document contains multiple shell script examples that implement various functionalities including an address book, marksheet generation, multiplication table, prime number display, Fibonacci series generation, palindrome check, and sorting numbers. Each script is designed to interact with the user through prompts and provides specific outputs based on user input. The examples demonstrate basic shell scripting techniques and control structures.

Uploaded by

Shobhit Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Shell programming: Write a program to implement an address book with options


given below:
a) Create address book. B) View address book. C) Insert a record. D) Delete a
record. e) Modify a record. F) Exit.
opt=1
while [ "$opt" -lt 6 ]
do

echo -e "Choose one of the Following\n1. Create a New Address Book\n2.


View Records\n3. Insert new Record\n4. Delete a Record\n5. Modify a Record\n6.
Exit"

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

Enter marks(out of 100) of


Subject 1: 10
Subject 2: 15
Subject 3: 10
Sum of marks of 3 subjects is : 35
Percentage: 11
sorry you failed! better luck next time
3. multiplication table

echo "Multiplication Table: "


echo "Which table do you want ? "
read num
iter=1
while [ $iter -le 10 ]
do
res=`expr $num \* $iter`
echo $num '*' $iter '=' $res
iter=`expr $iter + 1`
done

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

4. # Display prime Numbers in given range

echo "enter the range"


read n
echo "the prime no are:"
m=2
while [ $m -le $n ]
do
i=2
flag=0
while [ $i -le `expr $m / 2` ]
do
if [ `expr $m % $i` -eq 0 ]
then
flag=1
break
fi
i=`expr $i + 1`
done
if [ $flag -eq 0 ]
then
echo $m
fi
m=`expr $m + 1`
done

enter the range


19
the prime no are:
2
3
5
7
11
13
17
19

5. Shell Script which will generate first n Fibonacci numbers like: 1, 1, 2, 3, 5,


……
echo "How many number of terms to be generated ?"

read n

x=0 y=1 i=2

echo "Fibonacci Series up to $n terms :"

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=`echo $str | wc -c`

len=`expr $len - 1`

i=1

j=`expr $len / 2`

while test $i -le $j


do

k=`echo $str | cut -c $i`


l=`echo $str | cut -c $len`

if test $k != $l

then

echo "String is not palindrome"

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

echo "Your sorted data:"


sort -nr temp
echo "End of Script"

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy