Display Files with Line Numbers - knl

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Appendix D.  Sample Korn Shell Scripts


Display Files with Line Numbers - knl

This is a simple Korn shell version of the Unix nl command. It displays line-numbered output.

 #!/bin/ksh  #  #     knl - Korn Shell line-numbering filter  #  # Initialize line number counter  integer LNUM=1  # Check usage  if (($# == 0))  then        print "Usage: $0 file . . ."        exit 1  fi  # Process each file  for FILE  do        # Make sure file exists        if [[ ! -f $FILE ]]        then              print "$FILE: non-existent or not readable"              exit 1        else              # Open file for reading              exec 0<$FILE              # Read each line, print out with line number              while read -r LINE              do                    print "$LNUM: $LINE"                    ((LNUM+=1))              done        fi        # Reset line number counter        LNUM=1  done 

       
    Top
     



    Korn Shell. Unix and Linux Programming Manual, Third Edition
    Korn Shell. Unix and Linux Programming Manual, Third Edition
    ISBN: N/A
    EAN: N/A
    Year: 2000
    Pages: 177

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net