coreutils
[Top][All Lists]
Advanced

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

Re: stat(1) print formats


From: Assaf Gordon
Subject: Re: stat(1) print formats
Date: Fri, 21 Nov 2014 13:56:20 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0

Hello David,

On 11/21/2014 01:33 PM, Pádraig Brady wrote:
On 21/11/14 17:24, David Braun wrote:

1. The use of left and right quotes is counter to tradition and complicates
any shell post processing of the output. All shells that I'm familiar with
use paired quotes (left, right or double). Mixing them become difficult to
deal with.

That's been changed for quite a while with:
http://git.sv.gnu.org/gitweb/?p=gnulib.git;a=commitdiff;h=1a43a98

2. The use of quotes only occurs for the %N format and is only necessary
because of the "xxx -> yyy" result because of the possibility of special
characters in either of the xxx or yyy portions.

Perhaps an example of using C locale can help, also with shell-parsing.

    # Create a file and a symlink, with problematic characters
    $ touch "hello' wor"$'\n'ld
    $ ln -s hello* "hi' -> 'there"

    $ ls -log
    total 0
    -rw-rw-r-- 1  0 Nov 21 13:42 hello' wor?ld
    lrwxrwxrwx 1 13 Nov 21 13:42 hi' -> 'there -> hello' wor?ld


    # Using the default locale (in my case en_US.UTF-8)
    # Parsing will be challenging...
    $ stat -c %N h*
    ‘hello' wor\nld’
    ‘hi' -> 'there’ -> ‘hello' wor\nld’

    # Forcing C locale, the quotes are always single quotes,
    # and any quotes in the filenames are escaped:
    $ LC_ALL=C stat -c %N h*
    'hello\' wor\nld'
    'hi\' -> \'there' -> 'hello\' wor\nld'

Because the " -> " string is hard-coded in 'stat.c',
we can use it to split the string into src/dst filenames.
using the following shell commands:

    A=$(LC_ALL=C stat -c %N hi*)
    SRC=$(echo "$A" | sed "s/^'\(.*\)' -> '.*'$/\1/")
    DST=$(echo "$A" | sed "s/^'.*' -> '\(.*\)'$/\1/")
    echo "SRC ==$SRC=="
    echo "DST ==$DST=="

Will print:

    SRC ==hi\' -> \'there==
    DST ==hello\' wor\nld==

Though I wouldn't necessarily recommend using this in a production script - "find" + 
"-print0" is probably more robust.

HTH,
 - Assaf



reply via email to

[Prev in Thread] Current Thread [Next in Thread]