Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When, and how, to use xargs #17

Open
stuartpb opened this issue Jan 6, 2015 · 5 comments
Open

When, and how, to use xargs #17

stuartpb opened this issue Jan 6, 2015 · 5 comments

Comments

@stuartpb
Copy link

stuartpb commented Jan 6, 2015

Since this is apparently a somewhat obscure tool for a very necessary task (converting strings to argument lists while preserving quoting/grouping), I think it's called for to have a note on using xargs -x (and xargs -xa when you can't clobber STDIN) to deserialize arguments from strings/streams, involving how to interpolate multiple inputs to output your string as a stream (use a sequence of commands in a subshell).

@stuartpb
Copy link
Author

stuartpb commented Jan 6, 2015

Also, this is one of those places where a novice Basher may be tempted to use eval. Never use eval. Not only would it be opening yourself up to arbitrary code execution, it'd do the wrong thing: if someone passes ; exit 1 to a command that's going to append its arguments to an init script, passing this to eval would cause those to be interpreted as part of the command to be evaluated. This is why you let xargs interpret the arguments.

@progrium
Copy link
Owner

progrium commented Jan 6, 2015

Can you write something up on this? I'm not actually as familiar. Along with this, it could be nice to have a few useful patterns to showcase xargs.

@stuartpb
Copy link
Author

stuartpb commented Jan 7, 2015

The two best examples off the top of my head:

@stuartpb
Copy link
Author

stuartpb commented Jan 7, 2015

I think I wrote a few things on this in the issues for sshcommand. Basically the -x option to xargs tells it that we're constructing a single call (xargs is built to split very long streams into multiple calls by default), which should fail if it exceeds the size of a single call. (-x may also use an exec-like call to execute as a tail call, I'm not sure.) The -a option allows you to specify the arguments as a file (or a redirected process), so that STDIN can be inherited by the called function (if you use STDIN to specify the call's arguments, the launched process will get /dev/null as its STDIN).

@progrium
Copy link
Owner

I'm saying, can you write a little mini-guide on xarg patterns?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants