Vous êtes sur la page 1sur 26

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

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/sh echo prime nos between 1 and 30 are : echo 2 echo 3 i=4 while [ $i -le 30 ] do x=2 z=0 k=`expr $i / 2` while [ $x -le $k ] do l=`expr $i % $x` if test $l -eq 0 then z=1 fi x=`expr $x + 1` done if test $z -eq 0 then echo $i fi i=`expr $i + 1` done

EXECUTING prime.sh $ sh prime.sh prime nos between 1 and 30 are : 2 3 5 7 11 13 17 19 23 29

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/sh echo enter the 1st no. : read a x=$a echo enter the 2nd no. : read b y=$b i=1 p=1 while [ $i -le $y ] do p=`expr $p \* $x` i=`expr $i + 1` done echo $x raised to power $y = $p MAKING pow.sh EXECUTABLE $ chmod +x pow.sh EXECUTING pow.sh $ ./pow.sh enter the 1st no. : 2 enter the 2nd no. : 3 2 raised to power 3 = 8

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/sh i=1 while [ $i -le 10 ] do echo enter the no. of working hrs, of employee $i : read a b=$a if test $b -gt 40 then c=`expr $b - 40` c=`expr $c \* 100` echo overtime pay for employee $i = $c else echo there is no overtime pay for employee $i fi i=`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 :

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

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/sh set `date +%y` echo Enter year read yr rem1= `expr $yr % 100` rem2= `expr $yr % 4` if test $rem1 eq 0 then rem3= `expr $yr % 400` if test $rem3 eq 0 then echo $yr is a leap year else echo $yr is not a leap year fi elif test $rem2 eq 0 then echo $yr is a leap year else echo $yr is not a leap year fi

MAKING leapyr.sh EXECUTABLE $ chmod +x leapyr.sh EXECUTING leapyr.sh $ ./leapyr.sh Enter year 2008 2008 is a leap year $ ./leapyr.sh Enter year 2009 is not a leap year

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/sh Cat $1 > file l=0 w=0 while read file do l= `expr $1 + 1 done < file echo No. of lines = $l for i in `cat file` do w= `expr $w + 1` done echo No. of words = $w MAKING wcp.sh EXECUTABLE $ chmod +x wcp.sh EXECUTING wcp.sh $ ./wcp.sh Ibrar is a good boy Very cool No. of lines = 2 No. of words = 7

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, itll go ahead with it, otherwise it wont do. STUDENT NAME: Nitesh Gupta ENROLLMENT NO.: 0331042707 DATE OF COMPILATION: 12-09-09

CREATING A SHELL FILE: $ vi file.sh CODE: #!/bin/sh echo enter the name of source file read f1 echo enter the name of destination file read f2 if [ -f $f2 ] then echo destination file already exists cp -i $f1 $f2 else echo destination file does not exist fi MAKING file.sh EXECUTABLE $ chmod +x file.sh EXECUTING file.sh $ ./file.sh enter the name of source file ra enter the name of destination file ra1 destination file already exists cp: overwrite `ra1'? Y

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/sh for x in 1 2 3 do for y in 1 2 3 do for z in 1 2 3 do if [ $x ne $y ] then if [ $x ne $z ] then if [ $y ne $z ] then echo $x $y $z echo fi fi fi done done done

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

EXECUTING comp.sh $ ./comp.sh 123 132 213 231 321 312

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/sh x=0 echo ---------TERMINAL LOCKED--------- while [ $x eq 0 ] do read y echo Enter Password read t if [ $t = Rohit ] then x=1 else echo -----------WRONG PASSWORD---------- echo ---------TERMINAL LOCKED--------- fi done MAKING ter.sh EXECUTABLE $ chmod +x ter.sh EXECUTING ter.sh $ ./ter.sh ---------TERMINAL LOCKED-------Enter Password Rohit rohit@ubuntu$

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/sh echo Enter the name of source file read f1 echo "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

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/sh echo "*** 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 choice echo -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"

elif [ $choice -eq 4 ] then tf=$(echo "scale=2;(100000 * $tc)" |bc) echo "$tc kms = $tf cms" else echo "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 Meters 2. Convert Kilometers to Feets 3. Convert Kilometers to Inches 4. Convert Kilometers to Centimeters Select your choice (1-4) : 1 Enter Value of Kilometers b/w 2 cities: 6 6 kms = 6000 mts

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/sh echo Enter the digits: read n sum=0 while [ $n ne 0 ] do s= `expr $n % 10` sum= `expr $sum + $s` n= `expr $n / 10` done echo Sum of digits = $sum

MAKING sum.sh EXECUTABLE $ chmod +x sum.sh EXECUTING sum.sh $ ./sum.sh Enter the digits: 123456 Sum of digits = 21

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/sh echo "1. Area of Circle " echo "2. Area of Rectangle" echo "3. Area of Square" echo "Enter your choice: " read ch switch ($ch) case 1: echo Enter the Radius of a Circle : - read radius echo expr 22 \* $radius \* $rad / 7 echo Area of Circle:" echo echo 'expr 2 \* 22\* $radius /7' echo "Perimeter of Circle:" echo " " breaksw case 2: echo Enter length and breadth of a rectangle read length read breadth echo Area of Rectangle : echo expr $length \* $breadth echo 'expr 2 \* ($length\+ $breadth)' echo "Perimeter of Rectangle:" echo " " breaksw

case 3: echo Enter the side of a square read side echo 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 Rectangle 3. Area of Square Enter your choice: 2 Area of Rectangle : Enter length and breadth of a rectangle 3 3 Area of Rectangle : 9 Perimeter of Rectangle: 12

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/sh echo "input cost price" read c echo " input sale price" read s if [ $c -le $s ] ; then profit=$(((($s - $c) *100) / $c ))

echo " percentage profit is = $profit" else loss=$(((($c - $s) / 100) * $c )) echo "percentage loss is= $loss" fi MAKING conv.sh EXECUTABLE $ chmod +x cs.sh EXECUTING cs.sh $ ./cs.sh input cost price 123 input sale price 134 percentage profit is = 8

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/sh echo enter the number read n m=`expr $n % 2` if [ $m -eq 0 ] then echo number is even else echo number is odd fi MAKING evenodd.sh EXECUTABLE $ chmod +x evenodd.sh EXECUTING evenodd.sh $ ./evenodd.sh enter the number 4 number is even

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/sh echo Enter the number read n k=1 for((i=1;i<=10;i++)) do k=expr $n * i echo $n \* $i \= $k done echo \ done MAKING tabl.sh EXECUTABLE $ chmod +x tabl.sh EXECUTING tabl.sh $ ./ tabl.sh Enter the number 4 4*1=4 4*2=8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20

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/sh echo "enter range of prime nos Starts with a and ends at b" read a read b i=$a echo "the prime nos. b/w $a and $b are:" while [ $i -le $b ] do x=2 z=0 k=`expr $i / 2` while [ $x -le $k ] do l=`expr $i % $x` if test $l -eq 0 then z=1 fi x=`expr $x + 1` done if test $z -eq 0 then echo $i fi i=`expr $i + 1` done MAKING conv.sh EXECUTABLE

$ chmod +x cs.sh EXECUTING cs.sh $ ./cs.sh enter range of prime nos Starts with a and ends at b 105 210 the prime nos. b/w 105 and 210 are: 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199

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/sh s=0 y=0 m=1 echo "enter the year " read y if [ -z "$y" ] then echo "you have entered nothing" y=$(date +%Y) echo "using present year as $y" fi s=`expr $y % 4 ` if [ $s -eq 0 ] then echo "it is a leap year" else echo "not a leap year" fi for m in 1 3 5 7 9 11 do cal $m $y done MAKING conv.sh EXECUTABLE $ chmod +x pcal.sh EXECUTING pcal.sh $ ./ pcal.sh

enter the year 1234 not a leap year


Su 1 8 15 22 29 January Mo Tu We 2 3 4 9 10 11 16 17 18 23 24 25 30 31 1234 Th Fr 5 6 12 13 19 20 26 27 Sa 7 14 21 28

March 1234 Su Mo Tu We Th Fr 1 2 3 5 6 7 8 9 10 12 13 14 15 16 17 19 20 21 22 23 24 26 27 28 29 30 31 Su Mo 1 7 8 14 15 21 22 28 29 May 1234 Tu We Th 2 3 4 9 10 11 16 17 18 23 24 25 30 31 Fr 5 12 19 26

Sa 4 11 18 25

Sa 6 13 20 27

July 1234 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 September 1234 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 November Su Mo Tu We 1 5 6 7 8 12 13 14 15 19 20 21 22 26 27 28 29 1234 Th Fr 2 3 9 10 16 17 23 24 30 Sa 4 11 18 25

Vous aimerez peut-être aussi