Bash: Read a file, line by line


The following script will accept from the keyboard a filename, later it will read it and process it line by line.
In this example we will just number the lines, print them and count the total number of lines in the file.

#!/bin/sh
echo Enter the Filename to process
read filename
exec<$filename
lineNumber=0
while read line
do
	lineNumber=`expr $lineNumber + 1`;
	echo $lineNumber - $line;
done
echo "Total Lines Number: $lineNumber";

Leave a Reply