Thursday, December 19, 2013

Linux/Unix - Shell script to read a text file

Method 1 - By iterating each line using while loop

#!/bin/bash

#Name of the text file
txtFile=myFile.txt

echo "Reading text file "$txtFile" ..."

while read x
do
echo $x
done <$txtFile


Method 2 - By using cat (concatenate and display files) command

#!/bin/bash

#Name of the text file
txtFile=myFile.txt

echo "Reading text file "$txtFile" ..."

cat $txtFile