Old Man wrote:
> Old Man wrote:
>> Viatly wrote:
>>> The content of test.data is
>>>
>>> -bash-3.2# cat test.data
>>> line1
>>> line2
>>> line3
>>> It is important that there is no trailing EOL at the end of file.
>>> I read test.data with the following script:
>>>
>>> -bash-3.2# cat test.sh
>>> #!/bin/bash
>>> while read line
>>> do
>>> echo "$line"
>>> done < "test.data"
>>>
>>> -bash-3.2# ./test.sh
>>> line1
>>> line2
>>>
>>> That is, "line3" is lost.
>>> Questions:
>>> 1. What is a nice way to fix this code?
>>> 2. The code of the script pretends to be a stdandard way if reading
>>> text file line-by-line because it is recommended by respected
>>> resources (e.g. http://bash-hackers.org/wiki/doku.php/tests/bashfaq).
>>> Provided the code is ok, does it mean that a typical text file in Unix/
>>> Linux should have EOL at the end?
>>
>> First, your script will perfectly do the job.
>>
>> The read is terminating, possibly, because there is a character in the
>> input file that causes the read to think the end of the file has been
>> reached. Realize that UNIX does not have an EOF character; instead,
>> it has a total byte count. When the total bytes that are recorded in
>> the inode are read, then the file is deemed to be at the end of the
>> file. For your script to end at line 2 instead of line 3 means that
>> something caused it to think end of file.
>>
>> Inspect your input data in the file, test.data. I suspect that there
>> is a control character or something as simple as a control-c, carriage
>> return, or the like. To see if the input file has these values:
>>
>> strings test.data # Only shows valid printable characters.
>> od -cx test.data # Shows all values to determine the bad.
>> cat -vte test.data # Shows character values in characters;
>> # Shows no-character in another form such
>> # as a TAB is ^I.
>>
>> If the data was transferred from Windows to UNIX, these types of
>> non-visible characters are common.
>>
>> I hope that this ed.
>>
>> Old Man
>
>
> I omitted another tool that you need to make this evaluation.
>
> The command, "man ascii", shows the valid and visible characters, as
> well as the non-visible with their associated hex and such equivalent.
> All characters from hex 00 to 1F are non-visible; these are the ones
> that the od and cat commands will to see. If you have a problem
> value in test.data, it is likely in that range.
>
> Old Man
How-to-make-your-own-file-without-newline-at-the-end:
$ echo "line1" > file
$ echo "line2" >> file
$ echo -n "line3" >> file
and then
$ while read line; do echo $line; done < file
--
Best regards | Monica Lewinsky's X-Boyfriend's
Cyrus | Wife for President