Nitesh Linux

  • View
    18

  • Download
    2

Embed Size (px)

DESCRIPTION

Linux

Text of 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/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