You are on page 1of 32

1

INDEX
1. Greatest among three numbers..1 2. Prime numbers in a given range....2 3. Even and odd numbers..3 4. Fibonacci series.5 5. Palindrome or not..6 6. What type of character is input......7 7. Armstrong or not...8 8. Perfect or not.9 9. Magic number or not10 10. Combination of 1 2 311 11. Sum of digits of a number12 12. Leap year or not13 13. Factorial of a given number..14 14. One number raised to power of another number.......15 15. Student details ......16 16. Pay slip details...18 17. File permissions.19 18. Bubble sort.20 19. Binary search.22 20. Given file in proper format....24 21. Whether user logged in or not...25 22. Number of ordinary files and directories..26 23. File with maximum size....27 24. Change file permissions....28 25. File permission operations....30

1. Script to find greatest among three numbers. # GREATEST AMONG THREEE NUMBERS echo "To find Greatest among three numbers" echo "Enter a value:" read a echo "Enter b value:" read b echo "Enter c value:" read c if [ $a -ge $b -a $a -ge $c ] then echo "a=$a is Greater among $a,$b,$c" elif [ $b -gt $a -a $b -ge $c ] then echo "b=$b is Greater among $a,$b,$c" else echo "c=$c is Greater among $a,$b,$c" fi OUTPUT: [mca43@linuxserver mca43]$ sh lab1 To find Greatest among three numbers Enter a value: 45 Enter b value: 67 Enter c value: 99 c=99 is Greater among 45, 67, 99

2. Script to print Prime numbers in a given range. # PRIME NUMBERS IN A GIVEN RANGE echo "Prime numbers in a given series" echo "Enter range:" read range for ((i=1;i<range;i++)) do count=0 for ((j=2;j<i;j++)) do if ((i % j == 0)) then count=`expr $count + 1` fi done if ((count==0)) then echo "$i" fi done OUTPUT: [mca43@linuxserver mca43]$ sh lab2 Prime numbers in a given series Enter range: 15 1 2 3 5 7 11 13

3. Script to print Even and Odd numbers. # EVEN AND ODD NUMBERS IN A GIVEN RANGE echo "Even And Odd Numbers in a given Range:" echo "Enter range:" read n k=1 m=`expr $k % 2` echo "Odd numbers are" while [ $k -le $n ] do if [ $m -eq 0 ] then k=`expr $k + 2` fi echo "$k" k=`expr $k + 2` done k=2 echo "Even numbers are" while [ $k -le $n ] do echo "$k" k=`expr $k + 2` done

OUTPUT: [mca43@linuxserver mca43]$ sh lab3 Even And Odd Numbers in a given Range: Enter range: 10 Odd numbers are 1 3 5 7 9 Even numbers are 2 4 6 8 10

4. Script to print Fibonacci series. # FIBONACCI SERIES IN A GIVEN RANGE echo "Fibonacci Series" a=0 b=1 echo "Enter no.of Terms" read n while [ $n -ge 1 ] do c=`expr $a + $b` a=$b b=$c n=`expr $n - 1` echo "$c" done OUTPUT: [mca43@linuxserver mca43]$ sh lab4 Fibonacci Series Enter no.of Terms 10 1 2 3 5 8 13 21 34 55 89

5. Script to check whether the given string is Palindrome or not. # GIVEN STRING IS PALINDROME OR NOT if [ $# -lt 1 ] then echo "Usage $0 String" echo "Invalid arguments" exit fi cnt=`echo $1 |wc -c` s=$1 while [ $cnt -gt 0 ] do var=`echo $1 | cut -c $cnt` cnt=`expr $cnt - 1` temp=`echo $temp$var` done echo "The input string is $s" echo "The reversed string is $temp" if [ $s = $temp ] then echo "$s is a PALINDROME" else echo "$s is NOT A PALINDROME" fi OUTPUT: 1.[mca43@linuxserver mca43]$ sh lab5 Usage lab5 String Invalid arguments 2.[mca43@linuxserver mca43]$ sh lab5 AVIVA The input string is AVIVA The reversed string is AVIVA AVIVA is a PALINDROME

6. Script to check whether a given character is digit or alphabet or special character. #DIGIT OR CHARACTER OR SPECIAL CHARACTER echo "To know what type of character is given input" echo "Enter your input:" read ch case $ch in [a-z] |[A-Z])echo "$ch is an ALPHABET";; [0-9]) echo "$ch is a DIGIT";; ?) echo "$ch is a SPECIAL CHARACTER";; *) echo "You have entered more than one Character" esac OUTPUT: 1.[mca43@linuxserver mca43]$ sh lab6 To know what type of character is given input Enter your input: a a is an ALPHABET 2.[mca43@linuxserver mca43]$ sh lab6 To know what type of character is given input Enter your input: 7 7 is a DIGIT 3.[mca43@linuxserver mca43]$ sh lab6 To know what type of character is given input Enter your input: * * is a SPECIAL CHARACTER 4.[mca43@linuxserver mca43]$ sh lab6 To know what type of character is given input Enter your input: a2 You have entered more than one Character

7. Script to check whether the given number is Armstrong or not. # ARMSTRONG OR NOT echo "To know the given number is Armstrong or not" echo "Enter any number:" read n p=$n while [ $n -gt 0 ] do r=`expr $n % 10` sum=`expr $sum + $r \* $r \* $r` n=`expr $n / 10` done if [ $p = $sum ] then echo "The given number $p is Armstrong" else echo "The given number $p is Not Armstrong" fi OUTPUT: 1. [mca43@linuxserver mca43]$ sh lab7 To know the given number is Armstrong or not Enter any number: 153 The given number 153 is Armstrong 2. [mca43@linuxserver mca43]$ sh lab7 To know the given number is Armstrong or not Enter any number: 340 The given number 340 is Not Armstrong

10

8. Script to check whether the given number is Perfect or not. # NUMBER PERFECT OR NOt echo "To know if the given number is Perfect or not" echo "Enter any number" read n for((i=1;i<n;i++)) do if(($n % i==0)) then sum=`expr $sum + $i` fi done if(($n==$sum)) then echo "The given number $n is PERFECT" else echo "The given number $n is NOT PERFECT" fi OUTPUT: 1.[mca43@linuxserver mca43]$ sh lab8 To know if the given number is Perfect or not Enter any number 6 The given number 6 is PERFECT 2.[mca43@linuxserver mca43]$ sh lab8 To know if the given number is Perfect or not Enter any number 23 The given number 23 is NOT PERFECT

11

9. Script to check whether the given number is Magic number or not. #MAGIC OR NOT echo "To know if the given number is Magic or not" echo "Enter a number:" read n a=$n for((sum=0;a>0;a=`expr $a / 10`)) do r=`expr $a % 10` d=$r for((prod=1;d>0;d--)) do prod=`expr $prod \* $d` done sum=`expr $sum + $prod` done if [ $sum -eq $n ] then echo "The given number $n is MAGIC" else echo "The given number $n is NOT MAGIC" fi OUTPUT: 1.[mca43@linuxserver mca43]$ sh lab12 To know if the given number is Magic or not Enter a number: 145 The given number 145 is MAGIC 2.[mca43@linuxserver mca43]$ sh lab12 To know if the given number is Magic or not Enter a number: 234 The given number 234 is NOT MAGIC

12

10. Script to print Combination of 1 2 3 # COMBINATION OF 1 2 3 echo "The combination of 1 2 3" for i in 1 2 3 do for j in 1 2 3 do for k in 1 2 3 do if [ $i -ne $j -a $j -ne $k -a $k -ne $i ] then echo "$i $j $k" fi done done done OUTPUT: [mca43@linuxserver mca43]$ sh lab9 The combination of 1 2 3 123 132 213 231 312 321

13

11. Script to find sum of digits of a number. #SUM OF DIGITS OF A NUMBER echo "To find the sum of digits of a number" echo "Enter any number:" read n p=$n for((sum=0;n>0;n=n /10)) do r=`expr $n % 10` sum=`expr $sum + $r` done echo "Sum of digits of $p is $sum" OUTPUT: [mca43@linuxserver mca43]$ sh lab10 To find the sum of digits of a number Enter any number: 4567 Sum of digits of 4567 is 22

14

12. Script to find Leap year or not. # LEAP YEAR OR NOT echo "To know if the given year is leap year or not" echo "Enter the year:" read year if(($year % 4==0 && $year % 100!=0)) then echo "The given year $year is a LEAP YEAR" else echo "The given year $year is NOT A LEAP YEAR" fi OUTPUT: 1. [mca43@linuxserver mca43]$ sh lab11 To know if the given year is leap year or not Enter the year: 2004 The given year 2004 is a LEAP YEAR 2.[mca43@linuxserver mca43]$ sh lab11 To know if the given year is leap year or not Enter the year: 2005 The given year 2005 is NOT A LEAP YEAR

15

13. Script to find Factorial of any given number. #FACTORIAL OF A NUMBER echo "To find the Factorial of a number" fact=1 echo "Enter a number:" read n count=1 while [ $count -le $n ] do fact=`expr $fact \* $count` count=`expr $count + 1` done echo "Factorial of $n is $fact" OUTPUT: [mca43@linuxserver mca43]$ sh lab16 To find the Factorial of a number Enter a number: 6 Factorial of 6 is 720

16

14. Script to find the value of one number raised to the power of another. # ONE NUMBER AS THE POWER OF OTHER NUMBER echo "To find the power value of two numbers" echo "Enter a number:" read n echo "Enter another number" read m p=1 for((i=1;i<=m;i++)) do p=`expr $p \* $n` done echo "The value of $n to the power of $m is $p" OUTPUT: [mca43@linuxserver mca43]$ sh lab17 To find the power value of two numbers Enter a number: 2 Enter another number 4 The value of 2 to the power of 4 is 16

17

15. The marks obtained by a student in five different subjects are input through the keyboard. The student gets a division as per the following rules. % above or equal to 60 ..First division % between 50 and 59..Second division % between 40 and 49..Third division % less than 40..Fail. Write a script to calculate the division obtained by the student. #STUDENT DETAILS echo "STUDENT DETAILS" echo "Enter marks in five subjects:" read m1 read m2 read m3 read m4 read m5 sum=`expr $m1 + $m2 + $m3 + $m4 + $m5` avg=`expr $sum / 5` if [ $avg -lt 40 ] then echo "FAILED with an AVERAGE of $avg" elif [ $avg -le 59 -a $avg -ge 50 ] then echo "PASSED in SECOND DIVISION with an AVERAGE of $avg" elif [ $avg -le 49 -a $avg -ge 40 ] then echo "PASSED in THIRD DIVISION with an AVERAGE of $avg" else echo "PASSED in FIRST DIVISION with an AVERAGE of $avg" fi

18

OUTPUT: 1.[mca43@linuxserver mca43]$ sh lab13 STUDENT DETAILS Enter marks in five subjects: 78 89 67 77 60 PASSED in FIRST DIVISION with an AVERAGE of 74 2.[mca43@linuxserver mca43]$ sh lab13 STUDENT DETAILS Enter marks in five subjects: 34 45 20 34 45 FAILED with an AVERAGE of 35

19

16. Write a shell script to display the following details in a pay list Pay slip details. 1. House rent allowance. 2. Dearness allowance. 3. Provident fund. HRA is to be calculated at the rate of 20% of basic, DA at the rate of 40% of basic and PF at the rate of 10% of basic. # PAY SLIP DETAILS echo "Please enter your Basic:" read basic echo "PAY SLIP DETAILS" echo "1. HOUSE RENT ALLOWANCE" echo "2. DEARNESS ALLOWANCE" echo "3. PROVIDENT FUND" echo "your choice:" read ch case $ch in 1) hra=`expr $basic \* 20 / 100` echo Your HOUSE RENT ALLOWANCE is Rs. $hra;; 2) da=`expr $basic \* 40 / 100` echo Your DEARNESS ALLOWANCE is Rs. $da;; 3) pf=`expr $basic \* 10 / 100` echo Your PPOVIDENT FUND is Rs. $pf;; *) echo "Not a valid choice";; esac OUTPUT: 1.[mca43@linuxserver mca43]$ sh lab14 Please enter your Basic: 5890 PAY SLIP DETAILS 1. HOUSE RENT ALLOWANCE 2. DEARNESS ALLOWANCE 3. PROVIDENT FUND your choice: 1 Your HOUSE RENT ALLOWANCE is Rs. 1178

20

17. Write a shell script to display the file permission along with the file name which are to be accepted as command line arguments. # FILE PERMISSIONS echo "File Permissions" #usage $0 file1 file2 file3... var=$1 for k in $* do ls -l $1 | tr -s " " | cut -d " " -f1,9 shift done OUTPUT: [mca43@linuxserver mca43]$ sh lab15 lab12 lab18 college File Permissions -rw-rw-r-- lab12 -rw-rw-r-- lab18 total drwxrwxr-x courses

21

18. Script to sort the given list by using bubble sort. #BUBBLE SORT echo "Enter the range:" read n for((i=0;i<n;i++)) do echo "Enter the value for a[$i]:" read a[$i] done for((i=0;i<$n;i++)) do for((j=0;j<`expr $n - 1`;j++)) do if((${a[$j]}>${a[`expr $j + 1`]})) then t=${a[$j]} a[$j]=${a[`expr $j + 1`]} a[`expr $j + 1`]=$t fi done done echo "NUMBERS IN ASCENDING ORDER" for((i=0;i<$n;i++)) do echo "${a[$i]}" done

22

OUTPUT: [mca43@linuxserver mca43]$ sh lab18 Enter the range: 5 Enter the value for a[0]: 44 Enter the value for a[1]: 77 Enter the value for a[2]: 88 Enter the value for a[3]: 22 Enter the value for a[4]: 00 NUMBERS IN ASCENDING ORDER 00 22 44 77 88

23

19) Script to perform binary search echo "Enter the number of elements in the array" read n echo "Enter the $n elements in sorted order" for((i=0;i<$n;i++)) do read a[$i] done echo "Enter the element to be searched" read ele low=0 high=$n flag=0 while [ $low -le $high ] do sum=`expr $low + $high` mid=`expr $sum / 2` if [ ${a[$mid]} = $ele ] then flag=1 pos=`expr $mid + 1` break elif [ ${a[$mid]} -gt $ele ] then high=`expr $mid - 1` elif [ ${a[$mid]} -lt $ele ] then low=`expr $mid + 1` fi done if [ $flag = 1 ] then echo " The element is found at position $pos " else echo " The element not found in the array " fi

24

OUTPUT: [mca43@linuxserver mca43]$ sh binsch Enter the number of elements in the array 4 Enter the 4 elements in sorted order 20 15 10 5 Enter the element to be searched 10 The element is found at position 3

25

20) Script to receive file name & display the information about it in a proper format. clear echo "enter file name" read fname var=`ls -l|grep $fname` set - $var echo "name = $9" echo "file access permission = $1" echo "number of lines = $2" echo "number of file = $3" echo "group to which he belongs = $4" echo "size of file = $5" echo "file modification date = $6$7" echo "file modification time = $8" OUTPUT [mca43@linuxserver mca43]$sh file enter file name abc name = abc file access permission = -rw-rw-r-number of lines = 1 number of file = mca14 group to which he belongs = mca14 size of file = 21 file modification date = Aug18 file modification time = 12:07

26

21) Script to check whether the user logged or not, if logged display the user details. echo "enter the user ID" read s until who | grep $s do echo "not logged in" exit done temp=`grep $s /etc/passwd|cut -f1 -d":"` echo "user name is: $temp" tem=`grep $s /etc/passwd|cut -f2 -d":"` echo "password is: $tem" tc=`grep $s /etc/passwd|cut -f3 -d":"` echo "user ID is: $tc" t=`grep $s /etc/passwd|cut -f4 -d":"` echo "group ID is: $t" OUTPUT: [mca43@linuxserver mca43]$ sh l40 enter the user ID mca40 mca40 pts/26 Oct 29 11:48 (192.100.200.115)

27

22) Script to find number of ordinary file & directories. clear f=0 d=0 for file in * do if [ -d $file ] then d=`expr $d + 1` fi if [ -f $file ] then f=`expr $f + 1` fi done echo "number of files are:$f" echo "numbaer of directories are:$d" OUTPUT: [mca43@linuxserver mca43]$sh n_f number of files are:76 number of directories are:8

28

23) Script to find the file with maximum size. set `ls -l` shift 2 big=$5 for file in * do i=$5 if [ -f $file ] then if [ $i -gt $big ] then big=$i f=$file fi fi shift 9 done echo "max size file name is:$f" echo "file size:$big" OUTPUT: [mca43@linuxserver mca43]$ sh l11 max size file name is:l10 file size:1261

29

24) Script to change permissions of a file. echo "enter file name" read name if [ -f $name ] then echo "entered file is a regular file" echo "enter the class(user|group|others)" echo "enter u|g|o" read c case $c in U|u)echo "enter permission(read|write|execute)" echo "enter r|w|x" read p case $p in R|r)chmod u+r $name echo "read permissions to user class";; W|w)chmod u+w $name echo "write permissions to user class";; X|x)chmod u+x $name echo "execute permissions to user class";; *)echo "not a valid input" esac;; G|g)echo "enter permissions(read|write|execute)" echo "enter r|w|x" read p case $p in R|r)chmod g+r $name echo "read permissions to group class";; W|w)chmod g+w $name echo "write permissions to group class";; X|x)chmod g+x $name echo "execute permissions to group class";; *)echo "not a valid input";; esac;; O|o)echo "enter permissions(read|write|execute)" echo "enter r|w|x" read p case $p in R|r)chmod o+r $name

30

echo "read permissions to other class";; W|w)chmod o+w $name echo "write permissions to other class";; X|x)chmod o+x $name echo "execute permissions to other class";; *)echo "not a valid input";; esac;; *)echo "not a valid class" esac else echo "file does not exit" fi OUTPUT: [mca43@linuxserver mca43]$ sh l10 enter file name names entered file is a regular file enter the class(user|group|others) enter u|g|o u enter permission(read|write|execute) enter r|w|x x execute permissions to user class

31

25) Script to perform file creation, display, rename & deletion. echo "menu selection" echo "1.file creation" echo "2.file display" echo "3.file rename" echo "4.file delete" echo "enter ur choice" read ch case $ch in 1)echo "enter file name(to create)" read fname if [ -f $fname ] then echo "file exits" else echo "enter data into a file" echo "press ctr+d to end" cat>$fname echo "file created" fi;; 2)echo "enter file name" read fname if [ -f $fname ] then echo "the file $fname contents" cat $fname else echo "file does not exit" fi;; 3)echo "enter file name(to rename)" read fname echo "enter name to rename" read dname if [ -f $fname ] then mv $fname $dname echo "$fname file renamed to $dname" else echo "file does not exit"

32

fi;; 4)echo "enter file name to delete" read fname if [ -f $fname ] then rm $fname echo "filedeleted" else echo "file does not exit" fi;; *)echo "file does not exit" esac OUTPUT: [mca43@linuxserver mca43]$ sh l9 menu selection 1.file creation 2.file display 3.file rename 4.file delete enter ur choice 3 enter file name(to rename) names enter name to rename top names file renamed to top

You might also like