Subject: Re: bug#22128: dirname enhancement

From: Pádraig Brady <[email-masked]>
Date: Fri, 11 Dec 2015 18:38:15 +0000

On 11/12/15 14:46, Stephane Chazelas wrote:
> 2015-12-10 10:40:30 -0700, Bob Proulx:
> [...]
>> In this instance the first thing I thought of when I read your dirname
>> -f request was a loop.
>>
>>    while read dir; do dirname $dir; done < list
> 
> "read dir" expects the input in a very specific format and
> depends on the current value of IFS (like a dir called "my\dir "
> has to be input as "my\\dir\ " with the default value of IFS)
> and can't accept dir names with newline characters.
> 
> Invoking the split+glob operator on $dir doesn't make sense here
> unless you mean the input to be treated as a $IFS delimited list
> of patterns.
> 
> If the intention was to treat the input as a list of file
> paths, one per line (so can't do file paths with newline
> characters), then that would rather be:
> 
>  while IFS= read -r dir; do dirname -- "$dir"; done < list
> 
>>
>> Pádraig suggested xargs which was even shorter.
>>
>>   xargs dirname < filename
> 
> That expects yet another input format. That time, it can cope
> with any file path, since newline can be specified using quotes
> like:
> 
> "my dir
> with newline"
> 
> The output of dirname however won't be post-processable.

Both GNU basename and dirname since 8.16 (2012) got
the -z option to make the _output_ post-processable,
along with support for processing multiple inputs.

xargs splits arguments on the _input_ appropriately.
In general xargs is fine for this when the tool
doesn't need to process all inputs at once
(like sorting or generating a total for example).

cheers,
Pádraig.