On 8 March 2016 at 17:57, Criggie <[email protected]> wrote:
> I think the nifty thing was -n 3 for xargs. I was unaware it could do that.


The other cool trick with xargs is `-P n` where n is the number of
concurrent threads you want.

Its not really useful in your case, but its a cool trick thats easy to
overlook all the same.

I'd probably opt to something slightly more resilient, because all
sorts of fun things can happen if your input is not a multiple of 3 :)

I often find myself finding what other people would do with quick
duct-taping together of shell to be easier and safer implemented in
Perl.

Attached is my reinterpretation of the code you presented so far, but
hopefully its more obvious what I'm doing.

I also wouldn't be using code to generate shell that gets evaled
somewhere like you appear to be doing, because that's a nightmare when
you start talking filename escaping.

In the attached script, I'd be calling equivalent perl function calls
that actually removed the files ( which aren't subject to
interpolation/escaping headaches ) instead of merely printing them.

Assuming I have understood your code, the following script does as follows:

> echo -e "a\nb\nc\n" | perl /tmp/fdupes.pl
> # rm -f b c ; ln a b ; ln a c ;
> # Cant get destination 1 for '' at /tmp/fdupes.pl line 14, <> line 4.

That last line is sort the error case I was talking about, where the
trailing "\n" in echo leads to a blank line on the end of the file,
which in this case is a "source" file, but it couldn't determine the
subsequent "dest" files, so it bailed instead of writing useless code
to output.

ie: with a 4 line input, this happens in your code:

> echo -e "a\nb\nc\nd" | xargs -n 3 | awk ' { print  "rm -f " $2, $3  " ; ln " 
> $1 , $2 " ; ln " $1, $3 } '
> # rm -f b c ; ln a b ; ln a c
> # rm -f   ; ln d  ; ln d

That scares me a bit.

But the safer logic yeilds:

> echo -e "a\nb\nc\nd" | perl /tmp/fdupes.pl
> # rm -f b c ; ln a b ; ln a c ;
> # Cant get destination 1 for 'd' at /tmp/fdupes.pl line 12, <> line 4.



-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

Attachment: fdupes.pl
Description: Perl program

_______________________________________________
Linux-users mailing list
[email protected]
http://lists.canterbury.ac.nz/mailman/listinfo/linux-users

Reply via email to