2) Write A Menu Driven Shell Script Which Will Print The Following Menu and Execute The Given Task

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

2) Write a menu driven shell script which will print the following

menu and execute the given task.

clear

echo ---------------------------------------------

echo ' Menu Implementation'

echo ---------------------------------------------

echo 1.Display calendar of current month

echo 2.display todays date and time

echo 3.Display username those are currently

logged in the system

echo 4.Display your name

echo 5.Display your terminal number

echo 6. .remove from the terminal

echo Enter your choice

read choice

case $choice in

1) cal ;;

2) date;;

3) who;;

4) echo MSD;;

5) pwd ;;

6) exit ;;

*)echo “ invalid input “

Esac

OUTPUT :
PRACTICAL : 10

1) Write a shell script to validate the entered date

#!/bin/bash

# Shell program to find the validity of a given

date

dd=0

mm=0

yy=0

# store number of days in a month

days=0

# get day, month and year

echo -n "Enter day (dd) : "

read dd

echo -n "Enter month (mm) : "

read mm

echo -n "Enter year (yyyy) : "

read yy

# if month is negative (<0) or greater than 12

# then it is invalid month

if [ $mm -le 0 -o $mm -gt 12 ];

then

echo "$mm is invalid month."

exit 1

fi

# Find out number of days in given month

case $mm in

1) days=31;;
2) days=28 ;;

3) days=31 ;;

4) days=30 ;;

5) days=31 ;;

6) days=30 ;;

7) days=31 ;;

8) days=31 ;;

9) days=30 ;;

10) days=31 ;;

11) days=30 ;;

12) days=31 ;;

*) days=-1;;

esac

# find out if it is a leap year or not

if [ $mm -eq 2 ]; # if it is feb month then only

check of leap year

then

if [ $((yy % 4)) -ne 0 ] ; then

: # not a leap year : means do nothing and

use old value of days

elif [ $((yy % 400)) -eq 0 ] ; then

# yes, it's a leap year

days=29

elif [ $((yy % 100)) -eq 0 ] ; then

: # not a leap year do nothing and use old

value of days

else

# it is a leap year

days=29
fi

fi

# if day is negative (<0) and if day is more

than

# that months days then day is invaild

if [ $dd -le 0 -o $dd -gt $days ];

then

echo "$dd day is invalid"

exit 2

fi

# if no error that means date dd/mm/yyyy is

valid one

echo "$dd/$mm/$yy is a vaild date"

OUTPUT :
PRACTICAL NO : 9

1) Write a shell script to generate mark sheet of a student. Take 3


subjects, calculate and display total marks, percentage and
Class obtained by the student

echo "Enter the five subject marks for the student"

read m1

read m2

read m3

read m4

read m5

sum1=`expr $m1 + $m2 + $m3 + $m4 + $m5`

echo "Sum of 5 subjects are: " $sum1

per=`expr $sum1 / 5`

echo " Percentage: " $per

if [ $per -ge 60 ]

then

echo "You get Distinction”

elif [ $per -ge 50 ]

then

echo “You get First class”

elif [ $per -ge 40 ]

then

echo "You get Second class"

else

echo "You get Fail"


OUTPUT:
2) Write a shell script which will generate first n Fibonacci
numbers like: 1, 1, 2, 3, 5, 8, …

Algorithm:

Step 1: Start

Step 2: Declare variables

Step 3: initialize variables

Step 4: Read n from user

Step 5: Print x and y

Step 6: Repeat until I &lt;n

6.1 x= x + y

6.2print z

6.3x=y, y=z

6.4 i = i + 1

Step 7: Stop

#!/bin/bash

#program for fibonnaci series

Echo “enter input of a number:”

Read N

#first number of the Fibonacci series

a=0

#second number of the fibonacci series

b=1

echo “the fibonnaci series is:”

for((i=0 ; i<=N ; i++))

do
echo -n “$a”

fn=$((a + b))

a=$b

b=$fn

done

#end of the loop

OUTPUT :
PRACTICAL: 8

1)Write a shell script to check entered string is palindrome or not.

#!/bin/bash

Echo “enter a string to be entered :”

Read str

Echo

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

len=` expr $len | cut -c $len`

i=1

j= ` expr $len / 2`

while test $i -le $j

do

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

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

then

echo “String is not a palindrome”

exit

f1

i = ` expr $i + 1`

Len=`expr $len – 1`

done

echo “String is palindrome


OUTPUT :
2) Write a shell script to finds whether given number is prime or
not.

echo “enter a number”

read num

function prime

for((i=2 ; I <=num/2 ; i++))

do

If [ $((num % i)) -eq 0 ]


then

echo “$num is not a prime number”

exit

fi

done

echo “$num is a prime number.”

OUTPUT :
Name: Pratik Rajubhai Parmar

Enrollment No : 190180107039

Practical : 7

1) Write a shell script to find factorial of given number n.

#!/bin/bash

#Factorial of a given umber

Fact=1

echo enter the number:

Read n

for((i=1;i<=n ; i++))

do

Fact=`expr $fact \* $i`

Done

echo the Factorial of a number is $fact

OUTPUT :

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