Joshi
Joshi

Reputation: 631

Read line by line of a file in shell script where last line doesn't end with new line

I have a text file which contains the following lines:

7.3.0
12

I am using the below lines to read the content line by line

while read -r LINE; do
    if test $n -eq 1 
    then 
       var01=$LINE
    fi
    if test $n -eq 2 
    then 
       var02=$LINE
    fi
    n=`expr $n + 1`
done < <(tr -d '\r' < /mnt/Share/hpsum_build.txt)

the above code doesn't read 2nd line that is last line '12' which doesn't end with a new line. How to read the last line as well

Upvotes: 1

Views: 70

Answers (1)

anubhava
anubhava

Reputation: 784898

If your text line doesn't have an ending newline then you can do this at start of your script to force a newline in it:

echo '' >> /mnt/Share/hpsum_build.txt

You can also call dos2unix utility to remove DOS's \r before \n from each line.

Upvotes: 2

Related Questions