Вы находитесь на странице: 1из 29

login as: deepa deepa@10.250.0.11's password: Last login: Tue Apr 5 11:54:36 2011 from 10.250.0.

52 Q: Write Shell script to print HELLO on screen Program:[deepa@localhost shinchan]$ vi fi1.sh echo "HELLO"

Output:[deepa@localhost shinchan]$ sh fi1.sh HELLO

Q: Write Shell script to find whether number is positive or not. Program:-

echo "Enter Number" read n if [ $n -gt 0 ] then echo "$n number is positive" else echo "$n number is negative" fi

Output:[deepa@localhost shinchan]$ vi f3.sh [deepa@localhost shinchan]$ sh f3.sh Enter Number 12 12 number is positive

Q: Write Shell script to select an operating system to given list using nested ifelse. Program:osch=0 echo "1 unix"

echo "2 linux" echo -n "select ur OS choice [1 or 2]?" read osch if [ $osch -eq 1 ] then echo "You pick Unix" else if [ $osch -eq 2 ] then echo "You pick up linux" else echo "You don't choose any os" fi fi

Output: [deepa@localhost shinchan]$ sh f4.sh 1 unix 2 linux select ur OS choice [1 or 2]?1 You pick Unix

Q: Write Shell script to print WELCOME 5 times using for loop. Program:[deepa@localhost shinchan]$ vi for1.sh for i in 1 2 3 4 5 do echo "Welcome $i times" done

Output: [deepa@localhost shinchan]$ sh for1.sh Welcome 1 times Welcome 2 times Welcome 3 times Welcome 4 times Welcome 5 times

Q: Write a ShellScript to print Multiplication Table using For Loop. Program:echo "Multiplication Table" echo "Enter Number" read n for i in 1 2 3 4 5 6 7 8 9 10 do echo "$n * $i=`expr $i*$n|bc`" done

Output: [deepa@localhost shinchan]$ sh for2.sh Multiplication Table Enter Number 3 3 * 1=3 3 * 2=6 3 * 3=9 3 * 4=12 3 * 5=15 3 * 6=18 3 * 7=21 3 * 8=24 3 * 9=27 3 * 10=30

Q: Write Shell script using until to print the following 11111 22222 33333 44444 55555 Program:[deepa@localhost shinchan]$ vi for4.sh for((i=1;i<=5;i++)) do for((j=1;j<=5;j++)) do echo -n "$i"

done echo done

Output:[deepa@localhost shinchan]$ sh for4.sh 11111 22222 33333 44444 55555

Q: Write Shell script which changes the screen background to print chess board. Program:[deepa@localhost shinchan]$ vi chess.sh for((i=1;i<=9;i++)) do for((j=1;j<=9;j++)) do tot=`expr $i + $j` tmp=`expr $tot % 2` if [ $tmp -eq 0 ]; then echo -e -n " \033[47m " else echo -e -n " \033[40m " fi done echo -e -n " \033[40m "

echo done

Output:[deepa@localhost shinchan]$ sh chess.sh

Q: Write Shell script using while loop to check whether a number entered is palindrome or not Program:[deepa@localhost shinchan]$ vi palin.sh echo -n "Enter Number" read n sd=0 rev=" " on=$n while [ $n -gt 0 ] do sd=$(($n%10)) n=$(($n/10)) rev=$(echo ${rev}${sd}) done if [ $on -eq $rev ] then echo "Number is Palindrome" else echo "Not palindrome" fi

Output: [deepa@localhost shinchan]$ sh palin.sh Enter Number 101 Number is Palindrome [deepa@localhost shinchan]$

Q: Write Shell script using while loop to check whether a string entered is palindrome or not Program:[deepa@localhost shinchan]$ vi palins.sh echo "ENter String" read s rvs=`echo $s|rev` if [ $s = $rvs ] then echo "Palindrome" else echo "Not Palindrome" fi

Output:[deepa@localhost shinchan]$ sh palins.sh ENter String YMY Palindrome [deepa@localhost shinchan]$

Q:Write a shellscript to print N Natural No by reading n from user. Program:echo "Enter a Num" read n for((i=1;i<=n;i++)) do echo $i done

Output: [deepa@localhost shinchan]$ sh natural.sh Enter a Num 10 1 2 3 4 5 6 7 8 9 10

Q:Write a shellscript to print all the even numbers between 0 & 100, using until loop. Program:counter=0 until [ $counter -ge 100 ] do let counter+=2 echo $counter done

Output: [deepa@localhost shinchan]$ sh count1.sh 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36

38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

Q: Write Shell script to check whether a date entered is valid or not Program:[deepa@localhost shinchan]$vi date.sh dd=0 mm=0 yy=0 days=0 echo -n "Enter day(dd)" read dd echo -n "Enter Month(mm)" read mm echo -n "Enter year(yyyy)" read yy if [ $mm -le 0 -o $mm -gt 12 ] then echo "$mm is invalid month" exit 1 fi 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 if [ $mm -eq 2 ] then if [ $((yy%4)) -ne 0 ] then : elif [ $((yy%400)) -eq 0 ] then : days=29

elif [ $((yy%100)) -eq 0 ] then : else days=29 fi fi "date.sh" 49L, 645C

Output:[deepa@localhost shinchan]$ sh date.sh Enter day(dd)25 Enter Month(mm)1 Enter year(yyyy)2010 25/1/2010 is a valid date

Q: Write Shell script to print reverse of a number .

Program:[deepa@localhost shinchan]$ vi reverse.sh echo -n "ENter Number" read n sd=0 rev=" " while [ $n -gt 0 ] do sd=$(($n%10)) n=$(($n/10)) rev=$(echo ${rev}${sd}) done echo "$rev"

Output:[deepa@localhost shinchan]$ sh reverse.sh ENter Number 123 321

Q: Write Shell script to find sum of digit Program:-

[deepa@localhost shinchan]$ vi sum.sh echo -n "Enter a number" read n s=0 rem=" " while [ $n -gt 0 ] do rem=$(($n%10)) n=$(($n/10)) s=$(($s+$rem)) done echo "Sum Of Digits="$s

Output: [deepa@localhost shinchan]$ sh sum.sh Enter a number 541 Sum Of Digits=10

Q: Write Shell script to print following pattern using nested for loop. * ** *** **** *****

Program:[deepa@localhost shinchan]$ vi star.sh for ((i=1;i<=5;i++)) do for ((j=1;j<=i;j++)) do echo -n "*" done echo done

Output:[deepa@localhost shinchan]$ sh star.sh * ** *** **** *****

Q: Write Shell script to print following pattern using nested for loop. * ** *** **** ***** **** *** ** *

Program:[deepa@localhost shinchan]$ vi star1.sh echo "Enter A Number" read n for ((i=1;i<=$n;i++)) do for (( j=1;j<=i;j++)) do echo -n "*" done echo done for ((i=$n-1;i>=1;i--)) do for ((j=1;j<=i;j++)) do echo -n "*" done echo done

OUPTUT: [deepa@localhost shinchan]$ sh star1.sh Enter A Number 5 * ** *** **** ***** **** *** ** *

Q: Write Shell script which perform the basic arithmetic operation +,_,*,/ using case statement for ant two number given by user. Program:[deepa@localhost shinchan]$ vi arithop.sh read a echo -n "Enter 2nd Number" read b echo -n "Enter Ur Option \n1-Addition 2-Subtraction \n 3-Multiplication \n 4Division" read op case $op in 1)c=$(($a+$b)) echo $c;; 2)c=$(($a-$b)) echo $c;;

3)c=$(($a*$b)) echo $c;; 4)c=$(($a/$b)) echo $c;; *)echo "Invalid Option";; esac

Output:[deepa@localhost shinchan]$ sh arithop.sh Enter 1st Number 10 Enter 2nd Number 15 Enter Ur Option1-Addition 2-Subtraction 3-Multiplication 4-Division 1 25

Q: Write Shell script print prime number in certain range. Program:[deepa@localhost shinchan]$ vi primerange.sh echo "Enter a range" read rng echo "2" j=3 while [ $j -le $rng ] do i=2 x=`expr $j - 1` while [ $i -le $x ] do if [ `expr $j % $i` -ne 0 ] then i=`expr $i + 1` else break

fi done if [ $i -eq $j ] then echo $j fi j=`expr $j + 1` done Output:[deepa@localhost shinchan]$ sh primerange.sh Enter a range 10 2 3 5 7

Q: Write Shell script to check whether given number is prime number or not. Program:[deepa@localhost shinchan]$ vi prime.sh echo "Enter Number" read a typeset -i a typeset -i n typeset -i f f=0 if((a==2)) then echo $a "Is a prime no" elif((a<2)) then f=2 echo $a "Can't be evaluated" else n=2 while((n<(a/2))) do if((a%n==0))

then f=1 echo $a "Is not a prime no" break fi n=n+1 done fi if((f==0)) then echo $a "Prime No" fi

Output:[deepa@localhost shinchan]$ sh prime.sh Enter Number 11 11 Prime No [deepa@localhost shinchan]$

Q:Write a shellscript to print factorial of a no. Program:echo "Enter a Number" read n fact=1 for((i=1;i<=$n;i++)) do fact=`expr $fact*$i|bc` done echo "Factorial Of Number "$n" is :"$fact

Output:[deepa@localhost shinchan]$ sh fact.sh Enter a Number 6 Factorial Of Number 6 is :720

Q: The marks obtained by a student in 5 different subject are input through keyboard the student gets division as per following rules. % above or equal to 60 I Division % between 50 to 59 - II Division % between 40 to 49 - III Division % less than - Fail Write a program to calculate the division obtained by student Program:[deepa@localhost shinchan]$ vi SResult.sh echo "Enter Marks For DS,PNS,FM,CG,OS" read ds read pns read fm read cg read os typeset -i per typeset -i total total=$ds+$pns+$fm+$cg+$os per=100*$total/500 if [ $per -ge 60 ] then echo "1st Division" elif [ $per -ge 50 ] && [ $per -le 59 ] then echo "2nd Division" elif [ $per -ge 40 ] && [ $per -le 49 ] then echo "3rd division" else echo "Fail" fi

echo echo echo echo echo echo echo echo echo

" MARKSHEET " "**********************" "SUBJECT " "Max. Marks" "MARKS OBTAINED" "DATASTRUCTURE" "100 " $ds "PNS " "100 " $pns "FM " "100 " $fm "CG " "100 " $cg "OS " "100 " $fm "RESULT PASS"

Output: [deepa@localhost shinchan]$ sh SResult.sh Enter Marks For DS,PNS,FM,CG,OS 78 87 65 79 76 1st Division MARKSHEET ********************** SUBJECT Max. Marks MARKS OBTAINED DATASTRUCTURE 100 78 PNS 100 87 FM 100 65 CG 100 79 OS 100 65 RESULT PASS [deepa@localhost shinchan]$

Q: Write a menu driven shellScript to calculate area of circle,rectangle,square & triangle. Program:echo "Choose The correct OPtion" echo "1.To Calculate area of CIrcle" echo "2.To Calculate arae of RECTANGLE " echo "3.To Calculete area Of Square" echo "4.To Calculate Area Of Triangle" read opr case $opr in 1) echo "Enter radius of circle" read r area=`expr 3.14*$r*$r|bc` echo "Area Of Circle"$area ;; 2) echo "Enter length Of Rectangle" read len echo "Enter Breadth of Rectangle" read br area=`expr $len*$br|bc` echo "Area Of Rect"$area ;; 3) echo "Enter Side Of SQuare" read side area=`expr $side*$side|bc` echo "Area Of Square"$side ;; 4) echo "Enter height Of triangle" read h echo "Enter base Of triangle" read b area=`expr 0.5*$h*$b|bc` echo "Area Of Triangle"$area;; *)echo "You Have Choosen Wrong OPeration";; Esac

Output: [deepa@localhost shinchan]$ sh area.sh Choose The correct OPtion 1.To Calculate area of CIrcle 2.To Calculate arae of RECTANGLE 3.To Calculete area Of Square 4.To Calculate Area Of Triangle 2 Enter length Of Rectangle 5 Enter Breadth of Rectangle 6 Area Of Rect30

Q:Write a ShellScript to find length of string, concatenation of 2 string, to print string in reverse order. Program:echo "Choose the following Operation" echo "1.To Find length of string" echo "2.To Concatenate The String" echo "3.To print in reverse Order" read opr case $opr in 1)echo "Enter String" read str echo "Lentgh Of String is ${#str}";; 2)echo "ENter STring1" read str1 echo "Enter String2" read str2 echo "Concatenation Of 2 String:"$str1$str2;; 3) echo "Enter STring" read str result=`expr $str|rev` echo "REverse Of String"$result;; *)echo "You have choosen wrong operation"

Output:

[deepa@localhost shinchan]$ sh Strop.sh Choose the following Operation 1.To Find length of string 2.To Concatenate The String 3.To print in reverse Orders 2 ENter STring1 DEEPA Enter String2 JOSHI Concatenation Of 2 String:DEEPAJOSHI

Вам также может понравиться