59164221 Linux Programming File and Shell Scripting With Output Screens

download 59164221 Linux Programming File and Shell Scripting With Output Screens

of 18

Transcript of 59164221 Linux Programming File and Shell Scripting With Output Screens

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    1/18

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    2/18

    Index

    S. no Programs Page No Remark T. Sign

    1. Shell Commands 1

    2. Shell Commands for Processes 5

    3. File commands 6Shell Scripting

    4. Shell Script for Displaying a message 7

    5. To accept name, age of the user and display it onthe screen 8

    6. To find whether a number is even or odd 9

    7. To print the Fibonacci series 10

    8. To calculate the simple interest 11

    9. To calculate the factorial of a number 12

    10. To find whether a year is leap year or not 13

    11. To calculate the average of numbers given atcommand line 14

    12. To print a seven digit number in alternatesequence 15

    13.To count occurrence of a particular digit ininputted number 16

    14. Reverse a String 17

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    3/18

    Shell commands

    i) Command for making directoryroot@mca113:/# mkdir mca

    root@mca113:/# cd mca

    root@mca113:/mca# cat> PROJ.sem

    there are 60 students in PROJ sem

    root@mca113:/mca# cd PROJ

    there are 60 students in PROJ sem

    ii) Command for displaying calendar

    root@mca113:/# cal

    March 2011

    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

    iii) Command for displaying date

    root@mca113:/# date

    Tue Mar 29 13:11:10 IST 2011

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    4/18

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    5/18

    ix) Command for listing of contents of file

    root@mca113:/# ls

    a1 avearge.sh~ boot fact.sh home lib mnt niec proc sev test1 var

    a2 ave.sh cdrom fact.sh~ initrd.img lost+found naina occur.sh root sev~ test.cvmlinuz

    alter.sh ave.sh~ dev fib leap mca nancy occur.sh~ sbin srv tmpxyz.c

    avearge.sh bin etc fib~ leap~ media nancy.c opt selinux sys usr

    x) Command for knowing total process

    root@mca113:/# who

    root tty7 2011-03-29 11:32 (:0)

    root pts/0 2011-03-29 11:35 (:0.0)

    root pts/2 2011-03-29 12:59 (:0.0)

    xi) Command for knowing name of login person

    root@mca113:/# who am i

    root pts/2 2011-03-29 12:59 (:0.0)

    xi) Command for knowing user name of login person

    root@mca113:/# uname

    Linux

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    6/18

    Shell Commands for Processes

    i)Command to sleep a process

    root@mca112:/# sleep 100

    ii)Command to send a process in background

    root@mca112:/# bgbash: bg: current: no such job

    iii)Command to get a process in forground

    root@mca112:/# fgbash: fg: current: no such job

    iv)Command to kill a process

    root@mca112:/# killkill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l[sigspec]

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    7/18

    File commands

    i) Command for counting total number of character line and word in a file

    root@mca113:/# wc niec

    1 4 15 niec

    ii) Command for copying one file into another file

    root@mca113:/# cp niec mca

    iii)Command for knowing details of file

    root@mca113:/# file niec

    niec: ASCII text, with no line terminators

    iv)Command for renaming file

    root@mca113:/# mv niec neeta

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    8/18

    Shell Scripting

    Program Display welcome screen#WAP to display a simple welcome messageecho "WELCOME TO SHELL SCRIPT"

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    9/18

    Program To accept name, age of the user and display it on the screen

    #WAP to accept name and age of the user and display it on the screenecho "Enter the name of the person: "read nameecho "Now enter the age his age: "

    read ageecho "The name of the person is $name and his age is $age"

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    10/18

    Program To find whether a number is even or odd

    #WAP to find whether a number is even or odd

    echo "To find whether a number is even or odd"echo "Enter a number: "read numtst=`expr $num % 2`if [ $tst -eq 0 ]thenecho "The number you entered is even "elseecho "The number is odd"fi

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    11/18

    Program To print the Fibonacci series

    #shell script to generate fibonacci seriesecho "how many fibonnaci numbers do u want "read limita=0

    b=1d=1echo "____________________________________________________"echo -n $aecho -n " "while test $d -le $limitdoc=`expr ${a} + ${b}`echo -n $cecho -n " "

    b=$aa=$cd=`expr $d + 1`done

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    12/18

    Program To calculate the simple interest

    #WAP to calculate the simple interestecho "Enter the principle amount, rate of interest, time period(in months)"read principle

    read rateread timeecho "You entered: \n principle: $principle\n rate of interest: $rate\n time period: $time"

    prod=`expr $principle \\* $rate \\* $time`interest=`expr $prod / 1200`echo "The simple interest is: $interest"

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    13/18

    Program To calculate the factorial of a number

    #WAP to calculate the factorial of a number

    echo "Enter the number "read numecho "The factorial is "i=1fact=1while [ $i -le $num ]do

    fact=`expr $fact \\* $i`i=`expr $i + 1`

    doneecho "$fact"

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    14/18

    Program To find whether a year is leap year or not

    #WAP to find whether a year is leap year or notecho "Enter the year "read year tst=`expr $year % 4`if [ $tst -eq 0 ]thenecho "The year is leap"elseecho "The year is not a leap year"fi

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    15/18

    Program To calculate the average of numbers given at command line

    #WAP to calculate the average of numbers given at command lineecho "The average is: "sum=0i=0for x in $*do

    sum=`expr $sum + $x`i=`expr $i + 1`

    doneavg=`expr $sum / $i`echo "$avg"

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    16/18

    Program To print a seven digit number in alternate sequence

    #WAP to print a seven digit number in alternateecho " enter a seven digit number "read number len=`echo $number|wc -c`flag=1while [ $flag -le $len ]doecho $number|cut -c$flagflag=`expr $flag + 2`done

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    17/18

    Program To count occurrence of a particular digit in inputted number

    #shell script to count occurrence of a particular digit in inputted number"

    echo " enter any number :- "read numecho "enter the number to find occurence "read digitlen=`echo -n $num|wc -c`echo "the length of number is $len"count=0while [ $len -gt 0 ]doflag=`echo -n $num|cut -c $len`if [ $flag -eq $digit ]thencount=`expr $count + 1 `filen=`expr $len - 1`doneecho "$digit occured $count times in number($num)"

  • 8/6/2019 59164221 Linux Programming File and Shell Scripting With Output Screens

    18/18

    Program - Reverse a String

    echo -n "Enter a string "read string

    echo $stringlen=`echo -n $string|wc -c`echo "No of characters in the string $len "while [ $len -gt 0 ]dorev=`echo -n $string|cut -c $len`echo -n $rev

    len=`expr $len - 1`doneecho " "