On Fri, Sep 16, 2011 at 03:48, i...@whywouldwe.com <i...@whywouldwe.com>wrote:
> Doesn't xargs work, this would be the 'normal' way eg
>
> ls -1 | xargs -I var echo var
>
> compared to
>
> ls -1 | xargs echo
>
xargs -I doesn't work because it still splits the input on newlines
(possibly running the command multiple times, but I don't want that).
But xargs -0 does, I hadn't thought about that option yet so thanks. It
won't work if the input has embedded \0 characters, but c-based programs
can't handle them in command line arguments very well anyway.
On Sat, Sep 17, 2011 at 07:08, Robert Shinka <k...@unknownlayer.net> wrote:
> Notice how fish handily split the resulting string at newlines into
> separate array elements, and then expanded the result using spaces.
> For the typical case of processing data from line-oriented utilities
> like sed, this is exactly what you do /not/ want, as the simple act of
> storing data into a variable and retrieving it changes the data.
>
For the common case, I think this is what you want. In unix programs having
output in a form of one line per item is very common. For example filenames
containing spaces are handled correctly, unlike in bash. Try
set files (ls)
for i in $files
echo $i
end
(It still breaks on filenames containing newlines though.)
> You may be wondering what the point of the last two echo commands are.
> For the first, fish expands $var to "'1 2' '3' '' '4'", which echo then
> happily prints, while for the second, fish formats to "1 2 3 4" first.
> The first form can usually be handled with xargs (although this ideally
> wouldn't be necessary), but the second is broken any way I look at it.
>
> However, because there's no sane way to pipe the data to xargs (you
> weren't thinking of using echo, were you?) after retrieving it from a
> variable, you're reduced to the annoying hack you thought up earlier.
> Well, unless you decided to forego variables and instead dump everything
> into temporary files and use xargs -a ...
>
My specific problem isn't with variable/array handling, just with command
substitution. I don't see exactly what you think is wrong with "$var", if
you need $var or "$var", you can pick the right one. For command
substitution, this choice doesn't exist.
Bash treats command substitution and variable expansion as a list of
whitespace separated tokens. It also includes escapes to treat both as a
single token, in case you don't want the default behaviour.
Fish does the same for variables, except it doesn't split the contents when
the variable is used, but it stores the tokens in a structured way as an
array. Command substitution works a bit differently. Maintaining a
structured form of data like with variables isn't possible because it is
output from arbitrary commands, so fish splits the command output like bash
does, but only on newlines. However fish doesn't provide a way to prevent
the splitting.
Jan
------------------------------------------------------------------------------
BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA
http://p.sf.net/sfu/rim-devcon-copy2
_______________________________________________
Fish-users mailing list
Fish-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/fish-users