Nitesh Linux

34
PROGRAM AIM 3: Write a shell script to find factorial of a given number. STUDENT NAME: Nitesh Gupta ENROLLMENT NO.: 0331042707 DATE OF COMPILATION: 05-09-09 CREATING A SHELL FILE: $ vi fact.sh CODE: #!/bin/sh n=0 on=0 fact=1 echo -n "Enter number to find factorial:" read n on=$n while [ $n -ge 1 ] do fact=`expr $fact \* $n` n=`expr $n - 1` done echo "Factorial for $on is $fact" EXECUTING fact.sh $ sh fact.sh Enter number to find factorial: 4 Factorial for 4 is 24

description

Linux

Transcript of Nitesh Linux

Page 1: Nitesh Linux

PROGRAM AIM 3: Write a shell script to find factorial of a given number.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 05-09-09

CREATING A SHELL FILE:

$ vi fact.sh

CODE:

#!/bin/shn=0on=0fact=1echo -n "Enter number to find factorial:"read non=$nwhile [ $n -ge 1 ]do fact=`expr $fact \* $n` n=`expr $n - 1`doneecho "Factorial for $on is $fact"

EXECUTING fact.sh

$ sh fact.shEnter number to find factorial: 4Factorial for 4 is 24

Page 2: Nitesh Linux

PROGRAM AIM 4: Write a shell script to find all prime numbers between 1 and 30.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 05-09-09

CREATING A SHELL FILE:

$ vi prime.sh

CODE:

#!/bin/shecho prime nos between 1 and 30 are :echo 2echo 3i=4while [ $i -le 30 ]do

x=2z=0k=`expr $i / 2`while [ $x -le $k ]do

l=`expr $i % $x`if test $l -eq 0then

z=1fix=`expr $x + 1`

doneif test $z -eq 0then

echo $ifii=`expr $i + 1`

done

Page 3: Nitesh Linux

EXECUTING prime.sh

$ sh prime.shprime nos between 1 and 30 are :2357111317192329

Page 4: Nitesh Linux

PROGRAM AIM 5: Write a shell script that takes two numbers from keyboard and finds the value of one number raised to power of another number.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 05-09-09

CREATING A SHELL FILE:

$ vi power.sh

CODE:

#!/bin/shecho enter the 1st no. : read ax=$aecho enter the 2nd no. : read by=$b

i=1p=1while [ $i -le $y ]dop=`expr $p \* $x`i=`expr $i + 1`doneecho $x raised to power $y = $p

MAKING pow.sh EXECUTABLE$ chmod +x pow.shEXECUTING pow.sh $ ./pow.shenter the 1st no. :2enter the 2nd no. :32 raised to power 3 = 8

Page 5: Nitesh Linux

PROGRAM AIM 6: Write a shell script to calculate overtime pay for 10 employees. Overtime is paid at the rate of Rs 100 per hour for every hour worked above 40 hours. Assume that employees do not work for fractional part of an hour.STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 05-09-09

CREATING A SHELL FILE:

$ vi power.sh

CODE:

#!/bin/shi=1while [ $i -le 10 ]doecho enter the no. of working hrs, of employee $i :read ab=$aif test $b -gt 40thenc=`expr $b - 40`c=`expr $c \* 100`echo overtime pay for employee $i = $celseecho there is no overtime pay for employee $ifii=`expr $i + 1`done

MAKING over.sh EXECUTABLE$ chmod +x over.sh

EXECUTING over.sh $ ./over.sh

enter the no. of working hrs, of employee 1 :

Page 6: Nitesh Linux

2there is no overtime pay for employee 1enter the no. of working hrs, of employee 2 :44overtime pay for employee 2 = 400enter the no. of working hrs, of employee 3 :50overtime pay for employee 3 = 1000enter the no. of working hrs, of employee 4 :39there is no overtime pay for employee 4enter the no. of working hrs, of employee 5 :40there is no overtime pay for employee 5enter the no. of working hrs, of employee 6 :30there is no overtime pay for employee 6enter the no. of working hrs, of employee 7 :44overtime pay for employee 7 = 400enter the no. of working hrs, of employee 8 :38there is no overtime pay for employee 8enter the no. of working hrs, of employee 9 :51overtime pay for employee 9 = 1100enter the no. of working hrs, of employee 10 :46overtime pay for employee 10 = 600

Page 7: Nitesh Linux

PROGRAM AIM 7: Write a shell script which receives any year from the keyboard and determine whether the year is a leap year or not. If no argument is supplied the current year should be assumed.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 05-09-09

CREATING A SHELL FILE:

$ vi leapyr.sh

CODE:-

#!/bin/shset `date +%y`echo “Enter year”read yrrem1= `expr $yr % 100`rem2= `expr $yr % 4`if test $rem1 –eq 0then rem3= `expr $yr % 400`if test $rem3 –eq 0thenecho “$yr is a leap year”elseecho “$yr is not a leap year”fielif test $rem2 –eq 0thenecho “$yr is a leap year”elseecho “$yr is not a leap year”fi

Page 8: Nitesh Linux

MAKING leapyr.sh EXECUTABLE$ chmod +x leapyr.sh

EXECUTING leapyr.sh

$ ./leapyr.sh

Enter year20082008 is a leap year

$ ./leapyr.shEnter year

2009 is not a leap year

Page 9: Nitesh Linux

PROGRAM AIM 9: Write a shell script which counts the number of lines and words present in a file. If no file supplied at the command prompt then the input is taken from the keyboard.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 12-09-09

CREATING A SHELL FILE:

$ vi wcp.sh

CODE:-

#!/bin/shCat $1 > filel=0w=0while read filedol= `expr $1 + 1’done < fileecho “No. of lines = $l”for i in `cat file`dow= `expr $w + 1`doneecho “No. of words = $w”

MAKING wcp.sh EXECUTABLE$ chmod +x wcp.sh

EXECUTING wcp.sh

$ ./wcp.sh

Ibrar is a good boyVery cool

No. of lines = 2No. of words = 7

Page 10: Nitesh Linux

PROGRAM AIM 10: Write a shell script to copy a source file to destination file, if the destination file already exists, it should tell the user and then ask him (or her), if he/she wants to proceed with the copy or not. If the answer is “yes”, it’ll go ahead with it, otherwise it won’t do.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 12-09-09

CREATING A SHELL FILE:

$ vi file.sh

CODE:

#!/bin/shecho enter the name of source fileread f1echo enter the name of destination fileread f2if [ -f $f2 ]thenecho destination file already existscp -i $f1 $f2elseecho destination file does not existfi

MAKING file.sh EXECUTABLE

$ chmod +x file.sh

EXECUTING file.sh $ ./file.sh

enter the name of source fileraenter the name of destination filera1destination file already existscp: overwrite `ra1'? Y

Page 11: Nitesh Linux

PROGRAM AIM 11: Write a shell script to generate all combination of 1, 2 and 3 using for loops.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 12-09-09

CREATING A SHELL FILE:

$ vi comb.sh

CODE:-

#!/bin/shfor x in 1 2 3do

for y in 1 2 3 do

for z in 1 2 3do

if [ $x –ne $y ]then

if [ $x –ne $z ]then

if [ $y –ne $z ]then

echo $x $y $zecho “ “

fifi

fidone

donedone

Page 12: Nitesh Linux

MAKING comp.sh EXECUTABLE$ chmod +x comp.sh

EXECUTING comp.sh

$ ./comp.sh

1 2 31 3 22 1 32 3 13 2 13 1 2

Page 13: Nitesh Linux

PROGRAM AIM 12: Write a shell script which would display a message “Terminal Locked” on the screen and wait for the user to hit a key, it should accept a password and then unlock the terminal if the password matches.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 12-09-09

CREATING A SHELL FILE:

$ vi ter.sh

CODE:

#!/bin/shx=0echo “---------TERMINAL LOCKED---------“while [ $x –eq 0 ]doread yecho “Enter Password”read tif [ $t = “Rohit” ]thenx=1elseecho “-----------WRONG PASSWORD----------“echo “---------TERMINAL LOCKED---------“fidoneMAKING ter.sh EXECUTABLE$ chmod +x ter.sh

EXECUTING ter.sh $ ./ter.sh---------TERMINAL LOCKED--------Enter PasswordRohitrohit@ubuntu$

Page 14: Nitesh Linux

PROGRAM AIM 13: Write a shell script to find permission granted on a given file.STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 12-09-09

CREATING A SHELL FILE:

$ vi file.sh

CODE:

#!/bin/shecho “Enter the name of source file”read f1echo "Permissions on file you entered are as follows:" ls - l $f1

MAKING file.sh EXECUTABLE

$ chmod +x file.sh

EXECUTING file.sh $ ./file.sh

Enter the name of source file/home/students/csai97/mer/public_html/fi.txt

Permissions on file you entered are as follows:-rwxr-xr-- 1 rohit metal 512 Oct 23 19:23 fi.txt

Page 15: Nitesh Linux

PROGRAM AIM 14: Write a shell script to convert this distance in meters, feet, inches and centimeters.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 12-09-09

CREATING A SHELL FILE:

$ vi conv.sh

CODE:

#!/bin/shecho "*** Converting between the different measuring scales ***"echo "1. Convert Kilometers to Meters"echo "2. Convert Kilometers to Feets"echo "3. Convert Kilometers to Inches"echo "4. Convert Kilometers to Centimeters"echo -n "Select your choice (1-4) : "read choiceecho -n "Enter Value of Kilometers b/w 2 cities: "read tc if [ $choice -eq 1 ]then

tf=$(echo "scale=2;(1000 * $tc)" |bc)echo "$tc kms = $tf mts"

elif [ $choice -eq 2 ]then

tf=$(echo "scale=2;(3280.84 * $tc)" |bc)echo "$tc kms = $tf feets"

elif [ $choice -eq 3 ]then

tf=$(echo "scale=2;(39370.07 * $tc)" |bc)echo "$tc kms = $tf inches"

Page 16: Nitesh Linux

elif [ $choice -eq 4 ]then

tf=$(echo "scale=2;(100000 * $tc)" |bc)echo "$tc kms = $tf cms"

elseecho "Please select valid choice only"exit 1

fi

MAKING conv.sh EXECUTABLE$ chmod +x conv.sh

EXECUTING conv.sh $ ./conv.sh

*** Converting between the different measuring scales ***1. Convert Kilometers to Meters2. Convert Kilometers to Feets3. Convert Kilometers to Inches4. Convert Kilometers to Centimeters

Select your choice (1-4) :1Enter Value of Kilometers b/w 2 cities:66 kms = 6000 mts

Page 17: Nitesh Linux

PROGRAM AIM 15: Write a shell script which calculates the sum of digits of an integer number, when the number is input through the keyboard.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 12-09-09

CREATING A SHELL FILE:

$ vi sum.sh

CODE:

#!/bin/shecho “Enter the digits: “read nsum=0while [ $n –ne 0 ]dos= `expr $n % 10`sum= `expr $sum + $s`n= `expr $n / 10`doneecho “Sum of digits = $sum”

MAKING sum.sh EXECUTABLE$ chmod +x sum.sh

EXECUTING sum.sh

$ ./sum.sh

Enter the digits: 123456Sum of digits = 21

Page 18: Nitesh Linux

PROGRAM AIM 16: Write a shell script that calculates the area and perimeter of a circle, rectangle and square depend on user's choice.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 12-09-09

CREATING A SHELL FILE:

$ vi sel.sh

CODE:

#!/bin/shecho "1. Area of Circle "echo "2. Area of Rectangle"echo "3. Area of Square"echo "Enter your choice: "read chswitch ($ch) case 1:

echo “Enter the Radius of a Circle : - ”read radiusecho ‘expr 22 \* $radius \* $rad / 7’echo “Area of Circle:"echo “ ”echo 'expr 2 \* 22\* $radius /7'echo "Perimeter of Circle:"echo " "breakswcase 2:echo “Enter length and breadth of a rectangle”read lengthread breadthecho “Area of Rectangle : “echo ‘expr $length \* $breadth’echo 'expr 2 \* ($length\+ $breadth)'echo "Perimeter of Rectangle:"echo " "

Page 19: Nitesh Linux

breakswcase 3:echo “Enter the side of a square”read sideecho “Area of Square : “echo ‘expr $side \* $side’echo 'expr 4 \* side'echo "Perimeter of Square:"echo " "breaksw

endsw

MAKING sel.sh EXECUTABLE$ chmod +x sel.sh

EXECUTING sel.sh $ ./sel.sh

1. Area of Circle 2. Area of Rectangle3. Area of SquareEnter your choice: 2Area of Rectangle :Enter length and breadth of a rectangle33Area of Rectangle :9Perimeter of Rectangle:12

Page 20: Nitesh Linux

PROGRAM AIM 18: If cost price and selling price of an item is entered. Write a shell script which tells, which the profit or loss has passed and calculates the percentage of same(%loss and %profit).

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 19-09-09

CREATING A SHELL FILE:

$ vi cs.sh

CODE:#!/bin/shecho "input cost price"read cecho " input sale price"read sif [ $c -le $s ] ;thenprofit=$(((($s - $c) *100) / $c ))

echo " percentage profit is = $profit"elseloss=$(((($c - $s) / 100) * $c ))echo "percentage loss is= $loss"fi

MAKING conv.sh EXECUTABLE$ chmod +x cs.shEXECUTING cs.sh $ ./cs.sh

input cost price123input sale price134percentage profit is = 8

Page 21: Nitesh Linux

PROGRAM AIM 19: Write a shell script which tell whether the number is even or odd when the number is input through the keyboard.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 19-09-09

CREATING A SHELL FILE:

$ vi evenodd.sh

CODE:

#!/bin/shecho enter the numberread nm=`expr $n % 2`if [ $m -eq 0 ]thenecho number is evenelseecho number is oddfi

MAKING evenodd.sh EXECUTABLE$ chmod +x evenodd.sh

EXECUTING evenodd.sh $ ./evenodd.sh

enter the number4number is even

Page 22: Nitesh Linux

PROGRAM AIM 20: Write a shell script which generates the table of a given decimal integer, when the number is to specified by the user.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 19-09-09

CREATING A SHELL FILE:

$ vi tabl.sh

CODE:

#!/bin/shecho “Enter the number”read nk=1for((i=1;i<=10;i++))dok=’expr $n * i’echo “$n \* $i \= $k”doneecho \done

MAKING tabl.sh EXECUTABLE$ chmod +x tabl.sh

EXECUTING tabl.sh $ ./ tabl.sh

Enter the number4

4 * 1 = 44 * 2 = 84 * 3 = 124 * 4 = 164 * 5 = 20

Page 23: Nitesh Linux

PROGRAM AIM 21: Write a shell script to generate all the prime nos. in a given range input by user.STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 19-09-09

CREATING A SHELL FILE:

$ vi primeno.sh

CODE:

#!/bin/shecho "enter range of prime nos Starts with a and ends at b"read aread bi=$aecho "the prime nos. b/w $a and $b are:"while [ $i -le $b ]dox=2z=0k=`expr $i / 2`while [ $x -le $k ]dol=`expr $i % $x`if test $l -eq 0thenz=1fix=`expr $x + 1`doneif test $z -eq 0thenecho $i fi i=`expr $i + 1`done

MAKING conv.sh EXECUTABLE

Page 24: Nitesh Linux

$ chmod +x cs.sh

EXECUTING cs.sh $ ./cs.sh

enter range of prime nos Starts with a and ends at b105210the prime nos. b/w 105 and 210 are:107109113127131137139149151157163167173179181191193197199

Page 25: Nitesh Linux

PROGRAM AIM 22: Write a shell script which tells whether the year entered by the user is a leap year or not and displays the calendar of all the odd months of the year.

STUDENT NAME: Nitesh Gupta

ENROLLMENT NO.: 0331042707

DATE OF COMPILATION: 19-09-09

CREATING A SHELL FILE:

$ vi pcal.sh

CODE:

#!/bin/shs=0y=0m=1echo "enter the year "read yif [ -z "$y" ]thenecho "you have entered nothing"y=$(date +%Y)echo "using present year as $y"fis=`expr $y % 4 `if [ $s -eq 0 ]thenecho "it is a leap year"elseecho "not a leap year"fifor m in 1 3 5 7 9 11docal $m $ydone

MAKING conv.sh EXECUTABLE$ chmod +x pcal.sh

EXECUTING pcal.sh $ ./ pcal.sh

Page 26: Nitesh Linux

enter the year 1234not a leap year

January 1234 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31 March 1234 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30 31 May 1234 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728 29 30 31 July 1234 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930 31 September 1234 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 30 November 1234 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30