#fndar=($(IFS=$'\n'; find "$sdir" -type f -printf '%P|%TY-%Tm-%Td %TI:%TM|%s\n' | sort --version-sort --reverse))
the array construct ($( ... )) is using the space (between the date
and the time) also to split array elements,
sdir="$(pwd)"
#fndar=($(IFS=$'\n'; find "$sdir" -type f -printf '%P|%TY-%Tm-%Td %TI:%TM|%s\n' | sort --version-sort --reverse))
#fndar=($(IFS='\n'; find "$sdir" -type f -printf '%P|%TY-%Tm-%Td %TI:%TM|%s\n' | sort --version-sort --reverse))
fndar=($(find "$sdir" -type f -printf '%P|%TY-%Tm-%Td %TI:%TM|%s\n' |
sort --version-sort --reverse))
fndarl=${#fndar[@]}
echo "// __ \$fndarl: |${fndarl}|${fndar[0]}"
the array construct ($( ... )) is using the space (between the date
and the time) also to split array elements, but file names and paths
may contain spaces, so ($( ... )) should have a way to reset its
parsing metadata, or, do you know of any other way to get each whole
-printf ... line out of find as part of array elements?
Also, note that file names can also contain newlines in general. The
only robust delimiter is the NUL character.
On Fri, Dec 15, 2023 at 01:42:14PM +0100, Nicolas George wrote:
Also, note that file names can also contain newlines in general. The
only robust delimiter is the NUL character.
True. In order to be 100% safe, the OP's code would need to look
more like this:
readarray -d '' fndar < <(
find "$sdir" ... -printf 'stuff\0' |
sort -z --otherflags
)
The -d '' option for readarray requires bash 4.4 or higher. If this
script needs to run on bash 4.3 or older, you'd need to use a loop
instead of readarray.
This may look a bit inscrutable, but the purpose is to ensure that
a NUL delimiter is used at every step. First, find -printf '...\0'
will print a NUL character after each filename-and-stuff. Second,
sort -z uses NUL as its record separator (instead of newline), and
produces sorted output that also uses NUL. Finally, readarray -d ''
uses the NUL character as its record separator. The final result is
an array containing each filename-and-stuff produced by find, in the
order determined by sort, even if some of the filenames contain
newline characters.
Greg Wooledge (12023-12-15):
readarray -d '' fndar < <(
find "$sdir" ... -printf 'stuff\0' |
sort -z --otherflags
)
It is possible to do it safely in bash plus command-line tools, indeed.
But in such a complex case, it is better to use something with a
higher-level interface. I am sure File::Find and Version::Compare can
let Perl do the same thing in a much safer way.
Equally safe, perhaps. Not safer. I don't know those particular perl modules -- are they included in a standard Debian system, or does
one need to install optional packages? And then there's a learning
curve for them as well.
By the way, your MUA is adding 10000 years to its datestamps.
On Fri, Dec 15, 2023 at 02:30:21PM +0100, Nicolas George wrote:
Greg Wooledge (12023-12-15):
readarray -d '' fndar < <(
find "$sdir" ... -printf 'stuff\0' |
sort -z --otherflags
)
It is possible to do it safely in bash plus command-line tools, indeed.
But in such a complex case, it is better to use something with a higher-level interface. I am sure File::Find and Version::Compare can
let Perl do the same thing in a much safer way.
Equally safe, perhaps. Not safer. I don't know those particular perl modules -- are they included in a standard Debian system, or does
one need to install optional packages? And then there's a learning
curve for them as well.
By the way, your MUA is adding 10000 years to its datestamps.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 297 |
Nodes: | 16 (0 / 16) |
Uptime: | 00:55:00 |
Calls: | 6,669 |
Calls today: | 1 |
Files: | 12,216 |
Messages: | 5,338,493 |