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 counterinteger LNUM=1# Check usageif (($# == 0))thenprint "Usage: $0 file . . ."exit 1fi# Process each filefor FILEdo# Make sure file existsif [[ ! -f $FILE ]]thenprint "$FILE: non-existent or not readable"exit 1else# Open file for readingexec 0<$FILE# Read each line, print out with line numberwhile read -r LINEdoprint "$LNUM: $LINE"((LNUM+=1))donefi# Reset line number counterLNUM=1done