[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash read and field separator



On 09/12/2022 23:20, Greg Wooledge wrote:
On Fri, Dec 09, 2022 at 10:23:29PM +0700, Max Nikulin wrote:

find dir dir/ dir// -printf '%T@/%TY-%Tm-%Td/%TT/%p\0' | sort -zn |
     while IFS=/ read -rd '' _ day time path; do
         printf '%s %s %s\n' "$day" "${time%.*}" "$path";
     done | sed -n l
2022-12-09 22:13:14 dir$
2022-12-09 22:13:14 dir$
2022-12-09 22:13:14 dir//$

Single trailing / disappeared. Using any other character may cause incorrect
path in the output.

I can't reproduce your result.

unicorn:~$ mkdir tmp/leaf
unicorn:~$ find tmp/leaf tmp/leaf/ tmp/leaf// -printf '%T@/%TY-%Tm-%Td/%TT/%p\0' | while IFS=/ read -rd '' _ day time path; do printf '%s\n' "$path"; done
tmp/leaf
tmp/leaf/
tmp/leaf//

It took some time for me to realize why you case is different. Please, try

cd tmp
find leaf leaf/ leaf// -printf '%T@/%TY-%Tm-%Td/%TT/%p\0' | while IFS=/ read -rd '' _ day time path; do printf '%s\n' "$path"; done

I have found some speculations that it is standard behavior
https://stackoverflow.com/questions/52762210/shell-read-sometimes-strips-trailing-delimiter
Unfortunately I can not make similar conclusion after reading the cited documents. From my point of view
    set -- "$var"
is not the same as read. In the latter case remaining part of the line is assigned to the last variable. The argument that with preserved trailing separator it is impossible to express single field, does not work for read as well.

In dash I see the same behavior after dropping unsupported -d and using \n as record separator.


Reply to: