You are on page 1of 12

# 1. Write a shell script to accept two numbers and perform all arithmetic operations on it.

clear echo "Shell script for Arithmetic Operations" echo "Enter 1st Number : \c" read x echo "Enter 2nd Number : \c" read y clear ch=0 while [ \$ch -ne 6 ] do clear echo "1. Addition " echo "2. Subtraction " echo "3. Division" echo "4. Multiplication " echo "5. Modulo Division " echo "6. exit " echo " Enter Your Choice :\c" read ch case \$ch in 1) s=`expr \$x + \$y` echo " \$x + \$y = \$s " ;; 2) a=`expr \$x - \$y` echo " \$x - \$y = \$a" ;; 3) d=`expr \$x / \$y` echo "\$x / \$y = \$d" ;; 4) m=`expr \$x \* \$y` echo " \$x * \$y = \$m" ;; 5) r=`expr \$x % \$y` echo " \$x % \$y = \$r" ;; *) exit;; esac echo "Press any Key to Continuee .... \c" read rr done

2. Write a shell script to find largest of three numbers using conditional execution operators clear echo " Shell Script to find Largest among 3 Numbers " echo "Enter 1st Number :\c" read n1 echo "Enter 2nd Number :\c" read n2 echo "Enter 3rd Number :\c" read n3

if [ \$n1 -eq \$n2 -a \$n1 -eq \$n3 ] then echo \$n1 \$n2 \$n3 are Equal Numbers elif [ \$n1 -ge \$n2 -a \$n1 -ge \$n3 ] then echo \$n1 is Largest Number elif [ \$n2 -ge \$n3 -a \$n2 -ge \$n1 ] then echo \$n2 is Largest Number elif [ \$n3 -ge \$n1 -a \$n3 -ge \$n2 ] then echo \$n3 is Largest Number fi

3. Write a shell script to accept the name of the file from standard input and perform the following tests on it a) File executable b) File readable c) File writable d) Both readable & writable
clear echo "File Mode Test Script " echo "Enter File Name " read fn if [ ! -e \$fn ] then echo \$fn is Not Exists exit else echo \$fn is Exists fi ch=0 while [ \$ch -ne 5 ] do clear echo " 1. Executable Test " echo " 2. Readable Test " echo " 3. Writable Test " echo " 4. Readable & Writable Test " echo " 5. Exit " echo " Enter Your Choice :\c" read ch case \$ch in 1) if [ -x \$fn ] then echo \$fn is Executable File else echo \$fn is Not an Executable File fi

;; 2) if [ -r \$fn ] then echo \$fn is Readable File else echo \$fn is Not an Readable File fi ;; 3) if [ -w \$fn ] then echo \$fn is Writable File else echo \$fn is Not a Writable File fi ;; 4) if [ -r \$fn -a -w \$fn ] then echo \$fn is Both Readable and Writable File else echo \$fn is either Readble or Writable File fi ;; *)exit esac echo "Press Any Key to Continue... \c" read rr done

4.Write a shell script which will display the username and terminal name who login recently in to the Unix system.
clear echo "User Name-Terminal " echo "-------------------------" who | cut -f1,3 -d ' '|tr ' ' '-'

## # translates space with Hyphen

5.Write a shell script to find number of files in a directory clear echo "Enter a Directory File Name " read dn if [ ! -e \$dn ] then echo \$dn is an Invalid Directory File Name exit fi
cd \$dn x=`ls -l|grep '^-'|wc -l`

or Not Exists

## 6.Write a shell script to print the following format 1 12 123 1234 .

clear echo "Enter How many Rows read n k=1 while [ \$k -le \$n ] do j=1 s="" while [ \$j -le \$k ] do s=\$s\$j j=`expr \$j + 1` done echo "\$s" k=`expr \$k + 1` done :\c"

7. Write a shell script which will display the number of days in the given month and year
clear echo " Enter year : \c" read y echo " Enter Month [ 1 - 12] : \C" read m casE \$m in 1|3|5|7|8|10|12) echo "No. Of Days are 31";; 2) y1=`expr \$y % 4` y2=`expr \$y % 100` y3=`expr \$y % 400` if [ \$y3 -eq 0 -o \$y1 -eq 0 -a \$y2 -ne 0 ] then echo " No .of Days are 29" else echo " No . Of Days are 28 " fi ;; 4|6|9|11) echo " No . Of Days are 30";; *) echo "Invalid Month ";;

esac

8. Write a shell script to check whether a given number is perfect number or not
clear echo " Enter a Number to test Perfect or Not : \c" read n t=\$n k=1 s=0 while [ \$k -lt \$t ] do r=`expr \$t % \$k` if [ \$r -eq 0 ] then s=`expr \$s + \$k` fi k=`expr \$k + 1` done if [ \$s -eq \$n ] then echo \$n is a Perfect Number else echo \$n is Not a perfect number fi

## 9. Write a shell script for concatenation of two strings using arguments

clear if [ \$# -ne 2 ] then echo " Usage :sh 9.sh String1 String2 " exit fi echo String 1 Is \$1 echo String 2 Is \$2 j=\$1\$2 echo Concatenated String Is \$j

10. Write a shell script to demonstrate break and continue statements (count of Even Numbers in the Input) clear even=0 no=0 while true do echo "Enter a Number [ 0 to stop ] :\c"

read no if [ \$no -eq 0 ] then echo "Termination Input .Loop Breaks " break elif [ `expr \$no % 2` -eq 0 ] then even=`expr \$even + 1` else echo "Odd Number Input . Loop Continues...." continue fi done echo "Count of Even Inputs \$even "

11. Write a shell script to satisfy the following menu options a. Display current directory path b. Display todays date c. Display users who are connected to the Unix system d. Quit
clear echo " M E N U " ch=0 while [ \$ch -ne 4 ] do clear echo " 1. Current Directory Path" echo " 2. Todays Date " echo " 3. Logged Users " echo " 4. QUIT " echo " Enter Your Choice :\c" read ch case \$ch in 1) echo "Current Directory Path :" ;pwd;; 2) echo Todays Dats is :; date ;; 3) echo "Logged users List "; who ;; 4) exit;; *)exit esac echo "Press Any Key to Continue... \c" read rr done

12. Write a shell script to delete all files whose size is zero bytes from current directory clear

echo " Deleting files with Zero Size in the Current Directory " for k in `ls |find . -size 0` do echo \$k is Deleting .. rm \$k sleep 1 echo \$k is Deleted. done echo Files are deleted Successfully

13. Write a shell script to display reverse numbers from given argument list
clear for k in \$* do echo Given Number is \$k t=\$k rev=0 while [ \$t -ne 0 ] do rev=`expr \$rev \* 10 + \$t % 10` t=`expr \$t / 10` done echo Reverse Number is \$rev done Execution : \$ sh 13.sh 192 345 678

14. Write a shell script to display factorial value from given argument list
clear for k in \$* do echo Given Number is \$k t=1 fact=1 while [ \$t -le \$k ] do fact=`expr \$fact \* \$t` t=`expr \$t + 1` done echo Factorial for Given Number is \$fact done Execution : \$ sh 14.sh 3 5 9

15. Write a shell script which will greet you Good Morning, Good Afternoon, Good Evening and Good Night according to current time\
clear x=`date +%H` if [ \$x -lt 12 ] then echo " GOOD MORNING " elif [ \$x -lt 16 ] then echo " GOOD AFTERNOON " elif [ \$x -lt 19 ] then echo " Good Evening " else echo " GOOD NIGHT" fi

16. To implement the FCFS Algorithm # FCFS clear declare bt declare wt n=0 echo "Enter How Many process \c" read n # reading Burst or Service Time i=1 while [ \$i -le \$n ] do echo "Enter Burst Time for Process \$i :" read bt[\$i] i=`expr \$i + 1` done
# calculating Wating time,Total waiting Time and Average Waiting Time wt=0 twt=0 i=2 while [ \$i -le \$n ] do wt[\$i]=`expr \${bt[\$i-1]} + \${wt[\$i-1]}` twt=`expr \$twt + \${wt[\$i]}` i=`expr \$i + 1` done awt=`echo \$twt / \$n|bc -l`

#Displaying echo "Process Burst Time Waiting Time " echo " ------------------------------------ " i=1 while [ \$i -le \$n ] do echo " \$i \${bt[\$i]} \${wt[\$i]} " i=`expr \$i + 1` done echo " Total Wating Time :\$twt" echo " Average Waiting Time : \$awt"

## 17. To implement the Shortest Job First Algorithm

# SjF clear declare bt declare wt n=0 echo "Enter How Many process \c" read n # reading Burst or Service Time i=1 while [ \$i -le \$n ] do echo "Enter Burst Time for Process \$i :" read bt[\$i] i=`expr \$i + 1` done #sorting Basing on Short Burst Time i=1 while [ \$i -le \$n ] do j=`expr \$i + 1` while [ \$j -le \$n ] do if [ \${bt[\$i]} -gt \${bt[\$j]} ] then temp=\${bt[\$i]} bt[\$i]=\${bt[\$j]} bt[\$j]=\$temp fi j=`expr \$j + 1` done i=`expr \$i + 1` done

# calculating Wating time,Total waiting Time and Average Waiting Time wt=0 twt=0 i=2 while [ \$i -le \$n ] do wt[\$i]=`expr \${bt[\$i-1]} + \${wt[\$i-1]}` twt=`expr \$twt + \${wt[\$i]}` i=`expr \$i + 1` done awt=`echo \$twt / \$n|bc -l` # Displaying echo "Process Burst Time Waiting Time " echo " ---------------------------------------- " i=1 while [ \$i -le \$n ] do echo " \$i \${bt[\$i]} \${wt[\$i]} " #echo " Wating Time for Process \$i with Burst Time \${bt[\$i]} Is : \$ {wt[\$i]}" i=`expr \$i + 1` done echo " Total Wating Time :\$twt" echo " Average Waiting Time : \$awt"

18. To implement Priority Algorithm # Priority clear declare bt declare wt declare pr n=0 echo "Enter How Many process \c" read n
# reading Burst or Service Time and Priority i=1 while [ \$i -le \$n ] do echo "Enter Burst Time for Process \$i :" read bt[\$i] echo "Enter Priority value for Process \$i :" read pr[\$i] i=`expr \$i + 1` done #sorting Basing on Priority (Highest value wiht High Preference)

i=1 while [ \$i -lt \$n ] do j=`expr \$i + 1` while [ \$j -le \$n ] do if [ \${pr[\$i]} -lt \${pr[\$j]} ] then temp=\${bt[\$i]} pt=\${pr[\$i]} bt[\$i]=\${bt[\$j]} pr[\$i]=\${pr[\$j]} bt[\$j]=\$temp pr[\$j]=\$pt fi j=`expr \$j + 1` done i=`expr \$i + 1` done

# calculating Wating time,Total waiting Time and Average waiting Time wt=0 twt=0 i=2 while [ \$i -le \$n ] do wt[\$i]=`expr \${bt[\$i-1]} + \${wt[\$i-1]}` twt=`expr \$twt + \${wt[\$i]}` i=`expr \$i + 1` done awt=`echo \$twt / \$n|bc -l` # Displaying echo "Process Burst Time Priority Waiting Time " echo " ------------------------------------------------- " i=1 while [ \$i -le \$n ] do echo " \$i {wt[\$i]} " \${bt[\$i]} \${pr[\$i]} \$

#echo " Wating Time for Process \$i {wt[\$i]}" i=`expr \$i + 1` done echo " Total Wating Time :\$twt" echo " Average Waiting Time : \$awt"

## 19 . To implement Sequential File Organization

# Reading Data from Given file in Line by Line echo Enter File name read fn if [ -f \$fn ] then echo \$fn Contents are while read line do echo \$line done < \$fn else echo \$fn is not a Valid File Name fi

## 20 . To implement Randoml File Organization

# Reading Data from Given file basing a File echo Enter File name read fn if [ -f \$fn ] then echo Enter Line Number for Reading Contents read ln count=`cat \$fn|wc l` if [ \$ln gt \$count ] then echo Invalid Line Number.\$fn consists \$count Lines only exit else echo Requested Line Contents are head -\$ln \$fn|tee 1.txt | tail -1 1.txt fi else echo \$fn is An Invalid File Name fi