You are on page 1of 34

1

Examples of

UNIX SHELL PROGRAMS

Deepak Panth panth.deepak@gmail.com

2

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

3 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"

Script to print Prime numbers in a given range.4 fi OUTPUT: panth@deepak$ sh prog1 To find Greatest among three numbers Enter a value: 45 Enter b value: 67 Enter c value: 99 c=99 is Greater among 45.j++)) do if ((i % j == 0)) then count=`expr $count + 1` fi done if ((count==0)) then echo "$i" . 99 2.j<i.i++)) do count=0 for ((j=2. 67. # PRIME NUMBERS IN A GIVEN RANGE echo "Prime numbers in a given series" echo "Enter range:" read range for ((i=1.i<range.

# 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 . Script to print Even and Odd numbers.5 fi done OUTPUT: panth@deepak$ sh prog2 Prime numbers in a given series Enter range: 15 1 2 3 5 7 11 13 3.

6 echo "Even numbers are" while [ $k -le $n ] do echo "$k" k=`expr $k + 2` done OUTPUT: panth@deepak$ sh prog3 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 .

# 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: panth@deepak$ sh prog4 .7 4. Script to print Fibonacci series.

8 Fibonacci Series Enter no.1` temp=`echo $temp$var` done echo "The input string is $s" echo "The reversed string is $temp" . # 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 . Script to check whether the given string is Palindrome or not.of Terms 10 1 2 3 5 8 13 21 34 55 89 5.

9 if [ $s = $temp ] then echo "$s is a PALINDROME" else echo "$s is NOT A PALINDROME" fi OUTPUT: 1. *) echo "You have entered more than one Character" esac OUTPUT: 1.... ?) echo "$ch is a SPECIAL CHARACTER".panth@deepak$ sh prog5 Usage prog5 String Invalid arguments 2.panth@deepak$ sh prog6 To know what type of character is given input Enter your input: a a is an ALPHABET . #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". Script to check whether a given character is digit or alphabet or special character. [0-9]) echo "$ch is a DIGIT".panth@deepak$ sh prog5 AVIVA The input string is AVIVA The reversed string is AVIVA AVIVA is a PALINDROME 6.

panth@deepak$ sh prog6 To know what type of character is given input Enter your input: * * is a SPECIAL CHARACTER 4. Script to check whether the given number is Armstrong or not.panth@deepak$ sh prog6 To know what type of character is given input Enter your input: 7 7 is a DIGIT 3.panth@deepak$ sh prog6 To know what type of character is given input Enter your input: a2 You have entered more than one Character 7. # 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 .10 2.

Script to check whether the given number is Perfect or not. panth@deepak$ sh prog7 To know the given number is Armstrong or not Enter any number: 340 The given number 340 is Not Armstrong 8. panth@deepak$ sh prog7 To know the given number is Armstrong or not Enter any number: 153 The given number 153 is Armstrong 2. # NUMBER PERFECT OR NOt echo "To know if the given number is Perfect or not" echo "Enter any number" read n for((i=1.11 OUTPUT: 1.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 .i<n.

a=`expr $a / 10`)) do r=`expr $a % 10` d=$r for((prod=1.12 OUTPUT: 1. #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. Script to check whether the given number is Magic number or not.a>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" .d>0.panth@deepak$ sh prog8 To know if the given number is Perfect or not Enter any number 23 The given number 23 is NOT PERFECT 9.panth@deepak$ sh prog8 To know if the given number is Perfect or not Enter any number 6 The given number 6 is PERFECT 2.

panth@deepak$ sh prog12 To know if the given number is Magic or not Enter a number: 234 The given number 234 is NOT MAGIC 10.panth@deepak$ sh prog12 To know if the given number is Magic or not Enter a number: 145 The given number 145 is MAGIC 2.13 else echo "The given number $n is NOT MAGIC" fi OUTPUT: 1. 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: .

n=n /10)) do r=`expr $n % 10` sum=`expr $sum + $r` done echo "Sum of digits of $p is $sum" OUTPUT: panth@deepak$ sh prog10 To find the sum of digits of a number Enter any number: 4567 . Script to find sum of digits of a number.n>0. #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.14 panth@deepak$ sh prog9 The combination of 1 2 3 123 132 213 231 312 321 11.

# 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.15 Sum of digits of 4567 is 22 12. Script to find Leap year or not. panth@deepak$ sh prog11 To know if the given year is leap year or not Enter the year: 2004 .

16 The given year 2004 is a LEAP YEAR 2.panth@deepak$ sh prog11 To know if the given year is leap year or not Enter the year: 2005 The given year 2005 is NOT A LEAP YEAR 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: panth@deepak$ sh prog16 .

# 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++)) do p=`expr $p \* $n` done echo "The value of $n to the power of $m is $p" OUTPUT: panth@deepak$ sh prog17 To find the power value of two numbers .17 To find the Factorial of a number Enter a number: 6 Factorial of 6 is 720 14.i<=m. Script to find the value of one number raised to the power of another.

. #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 ] .Third division % less than 40……………….Second division % between 40 and 49……….. The marks obtained by a student in five different subjects are input through the keyboard.Fail...First division % between 50 and 59………. The student gets a division as per the following rules. % above or equal to 60 …….18 Enter a number: 2 Enter another number 4 The value of 2 to the power of 4 is 16 15. Write a script to calculate the division obtained by the student.

19 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 OUTPUT: 1.panth@deepak$ sh prog13 STUDENT DETAILS Enter marks in five subjects: 78 89 67 77 60 PASSED in FIRST DIVISION with an AVERAGE of 74 2.panth@deepak$ sh prog13 STUDENT DETAILS Enter marks in five subjects: 34 45 20 34 .

20 45 FAILED with an AVERAGE of 35 16. 2. HRA is to be calculated at the rate of 20% of basic. Provident fund. $hra. 1. # PAY SLIP DETAILS echo "Please enter your Basic:" read basic echo "PAY SLIP DETAILS" echo "1. 3. . DEARNESS ALLOWANCE" echo "3. HOUSE RENT ALLOWANCE" echo "2. DA at the rate of 40% of basic and PF at the rate of 10% of basic. Write a shell script to display the following details in a pay list Pay slip details. House rent allowance. PROVIDENT FUND" echo "your choice:" read ch case $ch in 1) hra=`expr $basic \* 20 / 100` echo Your HOUSE RENT ALLOWANCE is Rs.. Dearness allowance.

Write a shell script to display the file permission along with the file name which are to be accepted as command line arguments.21 2) da=`expr $basic \* 40 / 100` echo Your DEARNESS ALLOWANCE is Rs. *) echo "Not a valid choice"... $da.9 shift done OUTPUT: panth@deepak$ sh prog15 prog12 prog18 college File Permissions -rw-rw-r-. # FILE PERMISSIONS echo "File Permissions" #usage $0 file1 file2 file3. 1178 17.prog12 -rw-rw-r-. $pf. var=$1 for k in $* do ls -l $1 | tr -s " " | cut -d " " -f1.... esac OUTPUT: 1.panth@deepak$ sh prog14 Please enter your Basic: 5890 PAY SLIP DETAILS 1. HOUSE RENT ALLOWANCE 2. PROVIDENT FUND your choice: 1 Your HOUSE RENT ALLOWANCE is Rs. 3) pf=`expr $basic \* 10 / 100` echo Your PPOVIDENT FUND is Rs. DEARNESS ALLOWANCE 3.prog18 .

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

23 fi done done echo "NUMBERS IN ASCENDING ORDER" for((i=0.i++)) do echo "${a[$i]}" done OUTPUT: panth@deepak$ sh prog18 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 .i<$n.

24 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++)) 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 .i<$n.

25 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 OUTPUT: panth@deepak$ 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 .

clear echo "enter file name" read fname var=`ls -l|grep $fname` set .26 20) Script to receive file name & display the information about it in a proper format.$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 .

27 panth@deepak$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 21) Script to check whether the user logged or not. 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" . if logged display the user details.

115) 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" .200.100.28 OUTPUT: panth@deepak$ sh l40 enter the user ID mca40 mca40 pts/26 Oct 29 11:48 (192.

29 OUTPUT: panth@deepak$sh n_f number of files are:76 number of directories are:8 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" .

30 OUTPUT: panth@deepak$ sh l11 max size file name is:l10 file size:1261 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 .

.... X|x)chmod g+x $name echo "execute permissions to group class". *)echo "not a valid class" esac else echo "file does not exit" fi OUTPUT: panth@deepak$ sh l10 enter file name names entered file is a regular file . W|w)chmod g+w $name echo "write permissions to group class". *)echo "not a valid input" esac. X|x)chmod o+x $name echo "execute permissions to other class".. esac. W|w)chmod o+w $name echo "write permissions to other class"... esac. *)echo "not a valid input".. 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"....31 echo "execute permissions to user class".. *)echo "not a valid input". O|o)echo "enter permissions(read|write|execute)" echo "enter r|w|x" read p case $p in R|r)chmod o+r $name echo "read permissions to other class".

file display" echo "3. echo "menu selection" echo "1. display. rename & deletion.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" .32 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 25) Script to perform file creation.file rename" echo "4.file creation" echo "2.

file display .. 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" 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: panth@deepak$ sh l9 menu selection 1....file creation 2.33 fi.

file delete enter ur choice 3 enter file name(to rename) names enter name to rename top names file renamed to top .34 3.file rename 4.