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

Delhi kannda Educational societys

School of Computer Science


Linux Programs List Write a bash script 1. Write a shell script to print sum of prime numbers from 1 to 30? Answer-->echo "Write a shell script to print sum of prime numbers from 1 to 3 !" echo "" i"# echo "$he %ower %imit is&'i" b"3 echo "$he (pper %imit is&'b" sum" echo "" while ) 'i -le 'b * do for ++ ,"#-,.i-,//00 do if ) 1e,pr 'i 2 ',1 -e3 then break fi done if ) ', -e3 'i * then sum"1e,pr 'sum / 'i1 echo "'++i00 is prime number" fi *

i"1e,pr 'i / 11 done echo "" echo "$he sum of prime number from 1 to 3 is&- '++sum00" ANSW ! root4dinesh-laptop&56Desktop6shell7 !6prime!sh Write a shell script to print sum of prime numbers from 1 to 3 ! $he %ower %imit is&# $he (pper %imit is&3 # is prime number 3 is prime number 8 is prime number 9 is prime number 11 is prime number 13 is prime number 19 is prime number 1: is prime number #3 is prime number #: is prime number $he sum of prime number from 1 to 3 is&- 1#: ".Write a shell script to s#ap " numbers #ithout using thir$ %ariable using passing b& reference? echo "Write a shell script to swap # numbers without usin; third <ariable usin; passin; by reference!" echo "" a"1 echo "$he =alue for a is 'a" b"# echo "" echo "$he =alue for a is 'b" echo "'a and 'b" ' swap+a>b0

? a"1e,pr 'a / 'b1 b"1e,pr 'a - 'b1 a"1e,pr 'a - 'b1 return @ swap+a>b0 echo AAAfter Bwappin; AA echo "' swap+a>b0" echo " a is 'a" echo " b is 'b" ANSW ! root4dinesh-laptop&56Desktop6shell7 !6swap!sh Write a shell script to swap # numbers without usin; third <ariable usin; passin; by reference! $he =alue for a is 1 $he =alue for b is # 1 and # After Bwappin; a is # b is 1

3.Write a shell script to calculate maximum' minimum' a%erage an$ sum of 10 no. echo "Write a shell script to calculate ma,imum> minimum> a<era;e and sum of 1 no!" echo Enter four inte;ers with space between read a b c d e f ; h i C echo "" sum"1e,pr 'a / 'b / 'c / 'd / 'e / 'f / '; / 'h / 'i / 'C1

a<;"1e,pr 'sum 6 1 1 dec"1e,pr 'sum 2 1 1 echo Bum"'sum echo A<era;e"'a<;!'dec ANSW ! root4dinesh-laptop&56Desktop6shell7 !6a<;!sh Write a shell script to calculate ma,imum> minimum> a<era;e and sum of 1 no! Enter four inte;ers with space between 1#3D8E9F:1 Bum"88 A<era;e"8!8 (.)enerate a *ibonacci series of a gi%en number. echo "Generate a Hibonacci series of a ;i<en number!" if ) '7 -e3 1 * then Ium"'1 else echo -n "Enter a Iumber &" read Ium fi

f1" f#"1

echo "$he Hibonacci se3uence for the number 'Ium is & "

for ++ i" -i."Ium-i// 00 do

echo -n "'f1 " fn"'++f1/f#00 f1"'f# f#"'fn done echo "" ANSW ! root4dinesh-laptop&56Desktop6shell7 !6fib!sh Generate a Hibonacci series of a ;i<en number! Enter a Iumber &: $he Hibonacci se3uence for the number : is & 1 1 # 3 8 F 13 #1 3D +.,emperature of cit& in *ahrenheit $egrees is input through -e&boar$. Write a program to con%ert temperature from *ahrenheit to centigra$e $egrees . %ice %ersa. echo "JJJ Kon<ertin; between the different temperature scales JJJ" echo "1! Kon<ert Kelsius temperature into Hahrenheit" echo "#! Kon<ert Hahrenheit temperatures into Kelsius" echo -n "Belect your choice +1-#0 & " read choice

if ) 'choice -e3 1 * then echo -n "Enter temperature +K0 & " read tc 7 formula $f"+:680J$c/3# tf"'+echo "scale"#-++:680 J 'tc0 / 3#" Lbc0 echo "'tc K " 'tf H" elif ) 'choice -e3 # *

then echo -n "Enter temperature +H0 & " read tf 7 formula $c"+86:0J+$f-3#0 tc"'+echo "scale"#-+86:0J+'tf-3#0"Lbc0 echo "'tf " 'tc" else echo "Mlease select 1 or # only" e,it 1 fi ANSW ! root4dinesh-laptop&56Desktop6shell7 !6temp!sh JJJ Kon<ertin; between the different temperature scales JJJ 1! Kon<ert Kelsius temperature into Hahrenheit #! Kon<ert Hahrenheit temperatures into Kelsius Belect your choice +1-#0 & # Enter temperature +H0 & 1 D 1 D " 3:!E root4dinesh-laptop&56Desktop6shell7 !6temp!sh JJJ Kon<ertin; between the different temperature scales JJJ 1! Kon<ert Kelsius temperature into Hahrenheit #! Kon<ert Hahrenheit temperatures into Kelsius Belect your choice +1-#0 & 1 Enter temperature +K0 & D D K " 1 D! H /.An& &ear is entere$ through -e&boar$. Write a program to $etermine #hether &ear is leap or not. clear echo "Write a shell pro;ram is for checkin; the lear year or not" echo "" echo -n "Enter the year&-" read yr year"1e,pr 'yr N2 D1

year1"1e,pr 'yr N2 1 year#"1e,pr 'yr N2 D echo "" if + ) 'year -e3 then

1 1

* OO ) 'year1 -ne * LL ) 'year# -e3

*0

echo "$his is a leap year 'yr who ha<e 3EE days in a year " else echo "$his is not a leap year 'yr " fi echo "" ANSW ! Write a shell pro;ram is for checkin; the lear year or not Enter the year&-# F F who ha<e 3EE days in a year

$his is a leap year #

0.A positi%e integer is entere$ through -e&boar$. Write a function to fin$ the factorial of a gi%en number. clear echo "Write a shell pro;ram for calculatin; the factorial number" echo "" echo -n "Enter the factorial number&-" read fact echo "" i"1 while ) 'fact -;e 1 * do i"1e,pr 'i NJ 'fact1

fact"1e,pr 'fact - 11 done echo "$he result is &"'i ANSW ! Write a shell pro;ram for calculatin; the factorial number Enter the factorial number&-E $he result is &9#

1'2mplement the 3asic 4es- Calculator. clear echo "Write a shell pro;ram to demonstarte the basic mathematical calculations" echo "" echo "Enter the first <alue" read a echo "Enter the second <alue" read b echo ""

add"1e,pr 'a / 'b1 echo "$he addition of the first and the second <alue is&-"'add echo "" sub"1e,pr 'a - 'b1 echo "$he subtraction from the first and second <alue is&-"'sub echo "" mul"1e,pr 'a NJ 'b1 echo "$he product of first and second <alue is&-"'mul

echo "" di<"1e,pr 'a N6 'b1 echo "$he di<ision from first and second <alue is&-"'di< echo "" rim"1e,pr 'a N2 'b1 echo "$he reminder from first and second <alue is&-"'rim ANSW ! Write a shell pro;ram to demonstarte the basic mathematical calculations Enter the first <alue 1 Enter the second <alue F $he addition of the first and the second <alue is&-1F $he subtraction from the first and second <alue is&-# $he product of first and second <alue is&-F $he di<ision from first and second <alue is&-1 $he reminder from first and second <alue is&-# 5.Write a program to re%erse the gi%en number. clear echo "Write a shell pro;ram for re<ersin; the entered user input" echo "" echo -n "Enter the number&-" read num i"1 re<erse" while ) 'num -;t do *-

i"1e,pr 'num N2 1 1 re<erse"1e,pr 're<erse NJ 1 / 'i1 num"1e,pr 'num N6 1 1 done echo "" echo "$he user entered number in re<erse order is&-"'re<erse ANSW ! Write a shell pro;ram for re<ersin; the entered user input Enter the number&-1#3D8 $he user entered number in re<erse order is&-8D3#1 10.Write a program to print the %arious s&stems $ata li-e' 6ser Name' Shell' 7ome 4irector&' 4ate an$ ,ime. echo "Write a pro;ram to print the <arious systems data like> (ser Iame> Bhell> Pome Directory> Date and $ime!" echo "" echo "Hor checkin; the (ser Iame" hostname echo "" whoami echo "" echo "" echo "Hor checkin; the Pome Directory" pwd echo "" echo "Hor checkin; the Date" date

echo "" echo "Hor checkin; the $ime" time ANSW ! root4dinesh-laptop&56Desktop6shell7 !6systemdata!sh Write a pro;ram to print the <arious systems data like> (ser Iame> Bhell> Pome Directory> Date and $ime! Hor checkin; the (ser Iame dinesh-laptop root Hor checkin; the Pome Directory 6root6Desktop6shell Hor checkin; the Date Qon Rct 1D ##&D3&18 SB$ # 13 Hor checkin; the $ime real user sys m ! m ! m ! s s s

11. Sort the gi%en fi%e numbers in ascen$ing or$er. declare nos)8*"+D -1 # EE 1 0 echo "Rri;inal Iumbers in array&" for ++ i " - i ." D- i// 00 do echo '?nos)'i*@ done

for ++ i " - i ." D - i// 00 do for ++ C " 'i- C ." D- C// 00

do if ) '?nos)'i*@ -;t '?nos)'C*@ *- then t"'?nos)'i*@ nos)'i*"'?nos)'C*@ nos)'C*"'t fi done done echo -e "NnBorted Iumbers in Ascendin; Rrder&" for ++ i" - i ." D- i// 00 do echo '?nos)'i*@ done ANSW ! root4dinesh-laptop&56Desktop6shell7 !6sortasc!sh Rri;inal Iumbers in array& D -1 # EE 1 Borted Iumbers in Ascendin; Rrder& -1 # D 1 EE 1".Con%ert the gi%en $ecimal number into 7exa$ecimal number. n" he," echo "Decimal to he,adecimal con<erter <er! b !1"

echo -n "Enter number in decimal format & " read n he,"1echo "obase"1E-ibase"1 - 'n" L bc1 echo "'n is e3ui<alent N"'he,N" in he,adecimal" ANSW ! root4dinesh-laptop&56Desktop6shell7 !6he,!sh Enter number in decimal format & 1# 1# is e3ui<alent "K" in he,adecimal 13.An emplo&ee8s basic salar& is !s 1"+00. mplo&ee8s $earness allo#ance is +"9 of basic salar&' house rent allo#ance is 309 of basic salar&' an$ transport allo#ance is !s 1000. Write a script to calculate his gross salar&. echo -e "Enter ur basic salary Nc" read sal if ) 'sal -;e 1 then da"1e,pr 'sal NJ 8# 6 1 ha"1e,pr 'sal NJ 3 61 1 1 1 *

Isal"1e,pr 'sal / 'da / 'ha / 1 echo "(r Iet Balary is Ts! 'Isal " fi ANSW !

root4dinesh-laptop&56Desktop6shell7 !6;ross1!sh Enter ur basic salary E (r Iet Balary is Ts! 11:# 1(.,he $istance bet#een t#o cities :in -m.; is input through the -e&boar$. Write a program to con%ert an$ print this $istance in meters' feet' inches an$ centimeters. echo "Enter the distance in kilometers"

read dist

meters"1e,pr 'dist NJ 1

centi"1e,pr 'meters NJ 1

inches"1e,pr 'centi N6 #!8D L bc1

feet"1e,pr 'inches N6 1# L bc1

echo "Distance in meters " 'meters"

echo "Distance in Kentimeters " 'centi"

echo "Distance in inches " 'inches"

echo "Distance in feet " 'feet" ANSW ! Enter the distance in kilometers 1 Distance in meters " 1 Distance in Kentimeters " 1 Distance in inches " 8 Distance in feet " D1EE 1+.A cashier has currenc& notes of $enominations 10' +0 an$ 100. 2f the amount to be #ith$ra#n is input through the -e&boar$ in hun$re$s' fin$ the total number of currenc& notes of each $enomination the cashier #ill ha%e to gi%e to the #ith$ra#er. echo "Enter amount in multiple of hundred" read cash ten"1e,pr 'cash 6 1 1 fifty"1e,pr 'cash 6 8 1

hundred"1e,pr 'cash 6 1

echo "Kashier will ha<e to ;i<e 'ten ten rupees notes" echo "Kashier will ha<e to ;i<e 'fifty fifty rupees notes" echo "Kashier will ha<e to ;i<e 'hundred hundred rupees notes" ANSW !

1/.A librar& charges a fine for e%er& boo- returne$ late. *or first + $a&s the fine is +0 paise' for /<10 $a&s fine is one rupee an$ abo%e 10 $a&s fine is + rupees. 2f &ou return the boo- after 30 $a&s &our membership #ill be cancelle$. Write a program to accept the number of $a&s the member is late to return the boo- an$ $ispla& the fine or the appropriate message? echo "Enter Io! of Days & " read n if )'n -le 8 * echo "Uour Hine is !8 Maise" elif )'n -;t 8 * OO ) 'n -le 1 * echo "Uour Hine is Te! 1" elif )'n -;t 1 * OO ) 'n -le 3 * echo "Uour Hine is Te! 8" else echo "NnUour Qembership is Kancelled" @ ANSW ! Enter Io! of Days &18 Uour Hine is Te! 8
10.Write a program to print out all Armstrong numbers bet#een 1 an$ +00. 2f sum of cubes of each $igit of the number is e=ual to the number itself' then the number is calle$ an Armstrong number. *or example' 1+3 > :1 ? 1 ? 1; @ :+ ? + ? +; @ :3 ? 3 ? 3;

i"1 while++i."8 do c"'i d"'i b" a" while++c> 00 do a"'++c21 00 b"'++b / aJaJa00 c"'++c61 00 done if++b""d00- then echo "'i" fi i"'++i/100 done ANSW ! root4dinesh-laptop&56Desktop6shell7 !6arm!sh 1 183 39 391 D 9 00

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