O'Reilly logo

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

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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
				

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required