Unix Shell Programs

40
0 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 not……………………………………10 10. Combination of 1 2 3……………………………………11 11. Sum of digits of a number………………………………12 12. Leap year or not…………………………………………13 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

Transcript of Unix Shell Programs

Page 1: Unix Shell Programs

0

INDEX

1. Greatest among three numbers…………………………..12. Prime numbers in a given range………………………....23. Even and odd numbers…………………………………..34. Fibonacci series………………………………………….55. Palindrome or not………………………………………..66. What type of character is input………………………......77. Armstrong or not………………………………………...88. Perfect or not…………………………………………….9 9. Magic number or not……………………………………1010.Combination of 1 2 3……………………………………1111.Sum of digits of a number………………………………1212.Leap year or not…………………………………………1313.Factorial of a given number……………………………..1414.One number raised to power of another number…….......1515.Student details ………………………………………......1616.Pay slip details…………………………………………...1817.File permissions………………………………………….1918.Bubble sort……………………………………………….2019.Binary search…………………………………………….2220.Given file in proper format…………………………..…..2421.Whether user logged in or not…………………….……..2522.Number of ordinary files and directories………………..2623.File with maximum size………………………...……….2724.Change file permissions………………………...……….2825.File permission operations……………………..………..30

Page 2: Unix Shell Programs

1

1. Script to find greatest among three numbers.

# GREATEST AMONG THREEE NUMBERSecho "To find Greatest among three numbers"echo "Enter a value:"read aecho "Enter b value:"read becho "Enter c value:"read cif [ $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 lab1To find Greatest among three numbersEnter a value:45Enter b value:67Enter c value:99c=99 is Greater among 45, 67, 99

Page 3: Unix Shell Programs

2

2. Script to print Prime numbers in a given range.

# PRIME NUMBERS IN A GIVEN RANGEecho "Prime numbers in a given series"echo "Enter range:"read rangefor ((i=1;i<range;i++))do count=0for ((j=2;j<i;j++))do if ((i % j == 0))thencount=`expr $count + 1`fidoneif ((count==0))thenecho "$i"fidone

OUTPUT:[mca43@linuxserver mca43]$ sh lab2Prime numbers in a given seriesEnter range:15123571113

Page 4: Unix Shell Programs

3

3. Script to print Even and Odd numbers.

# EVEN AND ODD NUMBERS IN A GIVEN RANGEecho "Even And Odd Numbers in a given Range:"echo "Enter range:"read nk=1m=`expr $k % 2`echo "Odd numbers are"while [ $k -le $n ]do if [ $m -eq 0 ]then k=`expr $k + 2`fiecho "$k"k=`expr $k + 2`donek=2echo "Even numbers are"while [ $k -le $n ]doecho "$k"k=`expr $k + 2`done

Page 5: Unix Shell Programs

4

OUTPUT:[mca43@linuxserver mca43]$ sh lab3Even And Odd Numbers in a given Range:Enter range:10Odd numbers are13579Even numbers are246810

Page 6: Unix Shell Programs

5

4. Script to print Fibonacci series.

# FIBONACCI SERIES IN A GIVEN RANGEecho "Fibonacci Series"a=0b=1echo "Enter no.of Terms"read nwhile [ $n -ge 1 ]do c=`expr $a + $b` a=$b b=$c n=`expr $n - 1` echo "$c"done

OUTPUT:[mca43@linuxserver mca43]$ sh lab4Fibonacci SeriesEnter no.of Terms10123581321345589

Page 7: Unix Shell Programs

6

5. Script to check whether the given string is Palindrome or not.

# GIVEN STRING IS PALINDROME OR NOTif [ $# -lt 1 ]then echo "Usage $0 String" echo "Invalid arguments" exit ficnt=`echo $1 |wc -c`s=$1while [ $cnt -gt 0 ]do var=`echo $1 | cut -c $cnt` cnt=`expr $cnt - 1` temp=`echo $temp$var`doneecho "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 lab5Usage lab5 StringInvalid arguments

2.[mca43@linuxserver mca43]$ sh lab5 AVIVAThe input string is AVIVAThe reversed string is AVIVAAVIVA is a PALINDROME

Page 8: Unix Shell Programs

7

6. Script to check whether a given character is digit or alphabet or special character.

#DIGIT OR CHARACTER OR SPECIAL CHARACTERecho "To know what type of character is given input"echo "Enter your input:"read chcase $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 lab6To know what type of character is given inputEnter your input:aa is an ALPHABET2.[mca43@linuxserver mca43]$ sh lab6To know what type of character is given inputEnter your input:77 is a DIGIT3.[mca43@linuxserver mca43]$ sh lab6To know what type of character is given inputEnter your input:** is a SPECIAL CHARACTER4.[mca43@linuxserver mca43]$ sh lab6To know what type of character is given inputEnter your input:a2You have entered more than one Character

Page 9: Unix Shell Programs

8

7. Script to check whether the given number is Armstrong or not.

# ARMSTRONG OR NOTecho "To know the given number is Armstrong or not"echo "Enter any number:"read np=$nwhile [ $n -gt 0 ]do r=`expr $n % 10` sum=`expr $sum + $r \* $r \* $r` n=`expr $n / 10`doneif [ $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 lab7To know the given number is Armstrong or notEnter any number:153The given number 153 is Armstrong

2. [mca43@linuxserver mca43]$ sh lab7To know the given number is Armstrong or notEnter any number:340The given number 340 is Not Armstrong

Page 10: Unix Shell Programs

9

8. Script to check whether the given number is Perfect or not.

# NUMBER PERFECT OR NOtecho "To know if the given number is Perfect or not"echo "Enter any number"read nfor((i=1;i<n;i++))doif(($n % i==0))thensum=`expr $sum + $i`fidoneif(($n==$sum))thenecho "The given number $n is PERFECT"elseecho "The given number $n is NOT PERFECT"fi

OUTPUT:

1.[mca43@linuxserver mca43]$ sh lab8To know if the given number is Perfect or notEnter any number6The given number 6 is PERFECT

2.[mca43@linuxserver mca43]$ sh lab8To know if the given number is Perfect or notEnter any number23The given number 23 is NOT PERFECT

Page 11: Unix Shell Programs

10

9. Script to check whether the given number is Magic number or not.

#MAGIC OR NOTecho "To know if the given number is Magic or not"echo "Enter a number:"read na=$nfor((sum=0;a>0;a=`expr $a / 10`))dor=`expr $a % 10`d=$rfor((prod=1;d>0;d--))doprod=`expr $prod \* $d`donesum=`expr $sum + $prod`doneif [ $sum -eq $n ]thenecho "The given number $n is MAGIC"elseecho "The given number $n is NOT MAGIC"fi

OUTPUT:

1.[mca43@linuxserver mca43]$ sh lab12To know if the given number is Magic or notEnter a number:145The given number 145 is MAGIC

2.[mca43@linuxserver mca43]$ sh lab12To know if the given number is Magic or notEnter a number:234The given number 234 is NOT MAGIC

Page 12: Unix Shell Programs

11

10. Script to print Combination of 1 2 3

# COMBINATION OF 1 2 3echo "The combination of 1 2 3"for i in 1 2 3dofor j in 1 2 3dofor k in 1 2 3doif [ $i -ne $j -a $j -ne $k -a $k -ne $i ]thenecho "$i $j $k"fidonedonedone

OUTPUT:

[mca43@linuxserver mca43]$ sh lab9The combination of 1 2 31 2 31 3 22 1 32 3 13 1 23 2 1

Page 13: Unix Shell Programs

12

11. Script to find sum of digits of a number.

#SUM OF DIGITS OF A NUMBERecho "To find the sum of digits of a number"echo "Enter any number:"read np=$nfor((sum=0;n>0;n=n /10))dor=`expr $n % 10`sum=`expr $sum + $r`doneecho "Sum of digits of $p is $sum"

OUTPUT:

[mca43@linuxserver mca43]$ sh lab10To find the sum of digits of a numberEnter any number:4567Sum of digits of 4567 is 22

Page 14: Unix Shell Programs

13

12. Script to find Leap year or not.

# LEAP YEAR OR NOTecho "To know if the given year is leap year or not"echo "Enter the year:"read yearif(($year % 4==0 && $year % 100!=0))thenecho "The given year $year is a LEAP YEAR"elseecho "The given year $year is NOT A LEAP YEAR"fi

OUTPUT:

1. [mca43@linuxserver mca43]$ sh lab11To know if the given year is leap year or notEnter the year:2004The given year 2004 is a LEAP YEAR

2.[mca43@linuxserver mca43]$ sh lab11To know if the given year is leap year or notEnter the year:2005The given year 2005 is NOT A LEAP YEAR

Page 15: Unix Shell Programs

14

13. Script to find Factorial of any given number.

#FACTORIAL OF A NUMBERecho "To find the Factorial of a number"fact=1echo "Enter a number:"read ncount=1while [ $count -le $n ]do fact=`expr $fact \* $count` count=`expr $count + 1`doneecho "Factorial of $n is $fact"

OUTPUT:

[mca43@linuxserver mca43]$ sh lab16To find the Factorial of a numberEnter a number:6Factorial of 6 is 720

Page 16: Unix Shell Programs

15

14. Script to find the value of one number raised to the power of another.

# ONE NUMBER AS THE POWER OF OTHER NUMBERecho "To find the power value of two numbers"echo "Enter a number:"read necho "Enter another number"read mp=1for((i=1;i<=m;i++))do p=`expr $p \* $n`doneecho "The value of $n to the power of $m is $p"

OUTPUT:

[mca43@linuxserver mca43]$ sh lab17To find the power value of two numbersEnter a number:2Enter another number4The value of 2 to the power of 4 is 16

Page 17: Unix Shell Programs

16

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 DETAILSecho "STUDENT DETAILS"echo "Enter marks in five subjects:"read m1read m2read m3read m4read m5sum=`expr $m1 + $m2 + $m3 + $m4 + $m5`avg=`expr $sum / 5`if [ $avg -lt 40 ]thenecho "FAILED with an AVERAGE of $avg"elif [ $avg -le 59 -a $avg -ge 50 ]thenecho "PASSED in SECOND DIVISION with an AVERAGE of $avg"elif [ $avg -le 49 -a $avg -ge 40 ]thenecho "PASSED in THIRD DIVISION with an AVERAGE of $avg"elseecho "PASSED in FIRST DIVISION with an AVERAGE of $avg"fi

Page 18: Unix Shell Programs

17

OUTPUT:

1.[mca43@linuxserver mca43]$ sh lab13STUDENT DETAILSEnter marks in five subjects:7889677760PASSED 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

Page 19: Unix Shell Programs

18

16. Write a shell script to display the following details in a pay listPay 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 DETAILSecho "Please enter your Basic:"read basicecho "PAY SLIP DETAILS"echo "1. HOUSE RENT ALLOWANCE"echo "2. DEARNESS ALLOWANCE"echo "3. PROVIDENT FUND"echo "your choice:"read chcase $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 lab14Please enter your Basic:5890PAY SLIP DETAILS1. HOUSE RENT ALLOWANCE2. DEARNESS ALLOWANCE3. PROVIDENT FUNDyour choice:1Your HOUSE RENT ALLOWANCE is Rs. 1178

Page 20: Unix Shell Programs

19

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 PERMISSIONSecho "File Permissions"#usage $0 file1 file2 file3...var=$1for k in $*dols -l $1 | tr -s " " | cut -d " " -f1,9shiftdone

OUTPUT:

[mca43@linuxserver mca43]$ sh lab15 lab12 lab18 collegeFile Permissions-rw-rw-r-- lab12-rw-rw-r-- lab18totaldrwxrwxr-x courses

Page 21: Unix Shell Programs

20

18. Script to sort the given list by using bubble sort.

#BUBBLE SORTecho "Enter the range:"read nfor((i=0;i<n;i++))doecho "Enter the value for a[$i]:"read a[$i]donefor((i=0;i<$n;i++))dofor((j=0;j<`expr $n - 1`;j++))doif((${a[$j]}>${a[`expr $j + 1`]}))thent=${a[$j]}a[$j]=${a[`expr $j + 1`]}a[`expr $j + 1`]=$tfidonedoneecho "NUMBERS IN ASCENDING ORDER"for((i=0;i<$n;i++))doecho "${a[$i]}"done

Page 22: Unix Shell Programs

21

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

Page 23: Unix Shell Programs

22

19) Script to perform binary search

echo "Enter the number of elements in the array"read necho "Enter the $n elements in sorted order"for((i=0;i<$n;i++))do read a[$i]doneecho "Enter the element to be searched"read elelow=0high=$nflag=0while [ $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` fidoneif [ $flag = 1 ]then echo " The element is found at position $pos "else echo " The element not found in the array "fi

Page 24: Unix Shell Programs

23

OUTPUT:

[mca43@linuxserver mca43]$ sh binschEnter the number of elements in the array4Enter the 4 elements in sorted order2015105Enter the element to be searched10 The element is found at position 3

Page 25: Unix Shell Programs

24

20) Script to receive file name & display the information about it in a proper format.

clearecho "enter file name"read fnamevar=`ls -l|grep $fname`set - $varecho "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 fileenter file nameabcname = abcfile access permission = -rw-rw-r--number of lines = 1number of file = mca14group to which he belongs = mca14size of file = 21file modification date = Aug18file modification time = 12:07

Page 26: Unix Shell Programs

25

21) Script to check whether the user logged or not, if logged display the user details.

echo "enter the user ID"read suntil who | grep $sdoecho "not logged in"exitdonetemp=`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 l40enter the user IDmca40mca40 pts/26 Oct 29 11:48 (192.100.200.115)

Page 27: Unix Shell Programs

26

22) Script to find number of ordinary file & directories.

clearf=0d=0for file in *doif [ -d $file ]thend=`expr $d + 1`fiif [ -f $file ]thenf=`expr $f + 1`fidoneecho "number of files are:$f"echo "numbaer of directories are:$d"

OUTPUT:

[mca43@linuxserver mca43]$sh n_fnumber of files are:76number of directories are:8

Page 28: Unix Shell Programs

27

23) Script to find the file with maximum size.

set `ls -l`shift 2big=$5for file in *doi=$5if [ -f $file ]thenif [ $i -gt $big ]thenbig=$if=$filefifishift 9doneecho "max size file name is:$f"echo "file size:$big"

OUTPUT:

[mca43@linuxserver mca43]$ sh l11max size file name is:l10file size:1261

Page 29: Unix Shell Programs

28

24) Script to change permissions of a file.

echo "enter file name"read nameif [ -f $name ]thenecho "entered file is a regular file"echo "enter the class(user|group|others)"echo "enter u|g|o"read ccase $c inU|u)echo "enter permission(read|write|execute)"echo "enter r|w|x"read pcase $p inR|r)chmod u+r $nameecho "read permissions to user class";;W|w)chmod u+w $nameecho "write permissions to user class";;X|x)chmod u+x $nameecho "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 pcase $p inR|r)chmod g+r $nameecho "read permissions to group class";;W|w)chmod g+w $nameecho "write permissions to group class";;X|x)chmod g+x $nameecho "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 pcase $p inR|r)chmod o+r $name

Page 30: Unix Shell Programs

29

echo "read permissions to other class";;W|w)chmod o+w $nameecho "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"esacelseecho "file does not exit"fi

OUTPUT:

[mca43@linuxserver mca43]$ sh l10enter file namenamesentered file is a regular fileenter the class(user|group|others)enter u|g|ouenter permission(read|write|execute)enter r|w|xxexecute permissions to user class

Page 31: Unix Shell Programs

30

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 chcase $ch in1)echo "enter file name(to create)"read fnameif [ -f $fname ]thenecho "file exits"elseecho "enter data into a file"echo "press ctr+d to end"cat>$fnameecho "file created"fi;;2)echo "enter file name"read fnameif [ -f $fname ]then echo "the file $fname contents"cat $fnameelseecho "file does not exit"fi;;3)echo "enter file name(to rename)"read fnameecho "enter name to rename"read dnameif [ -f $fname ]thenmv $fname $dnameecho "$fname file renamed to $dname"elseecho "file does not exit"

Page 32: Unix Shell Programs

31

fi;;4)echo "enter file name to delete"read fnameif [ -f $fname ]thenrm $fnameecho "filedeleted"else echo "file does not exit"fi;;*)echo "file does not exit"esac

OUTPUT:

[mca43@linuxserver mca43]$ sh l9menu selection1.file creation2.file display3.file rename4.file deleteenter ur choice3enter file name(to rename)namesenter name to renametopnames file renamed to top