2007/09/19

Shell script "read file" howto

To read file in a shell script is very simple.

Referenced from http://ka1fsb.home.att.net/shfio.html

Reading Data into a File All at Once.

#----- The file to read
        myFile="/root/somefile"

#----- The "big" data variable
        myData=""

#----- Now the read
        myData=`cat $myFile`

#----- Show that the data is really in the variable...
#----- This is in the same format as the orginal file, new lines preserved
        echo "$myData"

#----- Show the data in non-quoted format, the space becomes the separator
        echo $myData

If you want to read data one line at a time, try this..
#----- The file to read
        myFile="/root/somefile"

#----- The line data variable
        myLine=""

#----- Loop to read file data content
        while [ 1 ]
        do
                read myLine || break
                echo "$myLine"

        done < $myFile