bugfindutils - Bugs: bug #51220, [wishlist] Using -I with -n

 
 

bug #51220: [wishlist] Using -I with -n

Submitter:  Adam Danischewski <adanisch>
Submitted:  Sun 11 Jun 2017 02:14:25 AM UTC
   
 
Category:  xargs Severity:  3 - Normal
Item Group:  None Status:  Working as Intended
Privacy:  Public Assigned to:  berny
Originator Name:  Open/Closed:  Closed
Release:  None Fixed Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Mon 12 Jun 2017 05:58:04 AM UTC, comment #6: 

I did not expect such enthusiastic feedback.
I read it as "works for me", so I'm hereby closing this issue.

Bernhard Voelker <berny>
Group administrator
Mon 12 Jun 2017 01:11:40 AM UTC, comment #5: 

[[spam content redacted]]

Anonymous
Mon 12 Jun 2017 01:09:51 AM UTC, comment #4: 

[[spam content redacted]]

Anonymous
Sun 11 Jun 2017 08:09:45 PM UTC, comment #3: 

The -I option already implies "-n 1" ... but - as POSIX [1]
says - the input is treated to be separated by newlines instead
of blanks.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/xargs.html

-I  replstr
    [XSI] [Option Start] Insert mode: utility is executed for each
    logical line from standard input. Arguments in the standard input
    shall be separated only by unescaped <newline> characters, not
    by <blank> characters. [...]

That's exactly what GNU xargs does; example:

  $ printf "%s\n" one two three four \
      | xargs -I{} echo cp -v "/tmp/{}" /tmp/test
  cp -v /tmp/one /tmp/test
  cp -v /tmp/two /tmp/test
  cp -v /tmp/three /tmp/test
  cp -v /tmp/four /tmp/test

I think it's documented well enough both in GNU xarg's man page
and (similarly) in the Texinfo manual:

 -I replace-str
     [...]  Also, unquoted blanks do not  terminate input items;
     instead the separator is the newline character. [...]

This is only missing for brevity in 'xargs --help' output.

Therefore, this is not a bug in the software, and implementing the
wish would clearly contradict to POSIX and break existing scripts.
Thus, I think I'll mark this as INVALID soon.

BTW: if you're sure to have GNU cp, then you could avoid the -I option
in the above example by using cp's --target option:

  $ echo some files | xargs cp -vt /tmp/test

Have a nice day,
Berny

Bernhard Voelker <berny>
Group administrator
Sun 11 Jun 2017 01:29:52 PM UTC, comment #2: 

The example doesn't seem to show that -I is incompatible with -n.  Indeed 'one two three four' isn't split into four arguments, but the documentation for -I says that it won't:

-I replace-str
       Replace occurrences of replace-str in the initial-arguments with
       names  read  from  standard input.  Also, unquoted blanks do not
       terminate input items; instead  the  separator  is  the  newline
       character.  Implies -x and -L 1.

Dale Worley <worley>
Sun 11 Jun 2017 01:24:18 PM UTC, comment #1: 

It would help if you simplified the example.  In this case, make the variable substitutions.  And instead of using "cp" as the command which is executed, thus showing us only the error message "cp" reports and leaving us to guess what the command was, use "echo" as the command.

That would give an example like this:
echo "one two three four" |
xargs -n1 -I{} echo cp -v "/tmp/{}" /tmp/test

Dale Worley <worley>
Sun 11 Jun 2017 02:14:25 AM UTC, original submission:  

It would be nice if you could use -I substitution option with -n count option together in the same command.

E.g.
$> var=/tmp && echo "one two three four" | xargs -n1 touch "${var}/{}"

$> var=/tmp/test && mkdir -p "${var}" && echo "one two three four" | xargs -n1 | xargs -I{} cp -v "/tmp/{}" "${var}"
‘/tmp/one’ -> ‘/tmp/test/one’
‘/tmp/two’ -> ‘/tmp/test/two’
‘/tmp/three’ -> ‘/tmp/test/three’
‘/tmp/four’ -> ‘/tmp/test/four’

Yet it would be nice if instead, we could:
$> var=/tmp/test && mkdir -p "${var}" && echo "one two three four" |xargs -n1 -I{} cp -v "/tmp/{}" "${var}"
cp: cannot stat ‘/tmp/one two three four’: No such file or directory

Unfortunately we can't combine -n and -I substitution at the moment and I'm not sure why not. It could be useful.



Adam Danischewski <adanisch>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by berny (Posted a comment)
  • -email is unavailable- added by worley (Posted a comment)
  • -email is unavailable- added by adanisch (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2017-06-12 berny StatusNone Working as Intended
        Open/ClosedOpen Closed
    2017-06-11 berny CategoryNone xargs
        Assigned toNone berny

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code