perigee perigee - 1 year ago 64
Bash Question

shell, pipe a list of arguments one by one to command

I have a list arguments needed to be passed one by one to another command, since the command only accepts one argument at time, any shell pipe I can use? Thank you in advance.

For example,

$ ls -al | awk '{print $9}'

the command will return a list of folder names, then I need send these names as arguement to anther command, which only accepts on command per execution.

Answer Source

I don't understand the ls -al | awk ... analogy. What is the point of including this code example?

If you have a whitespace delimited list of arguments, using a utility command like xargs will supply each argument to a command one-by-one by default. You can change this default behavior, but it doesn't seem like that you'd need to. You can use xargs as follows...

ls -al | awk '{print $9}' | xargs cat cat the contents of each file output by the ls -al command. Does this help?

It should be noted though that ls is a porcelain command and not a plumbing command. In other words, it is best practice to not rely on the output of ls for a command pipeline. A utility like find, however, is a plumbing command and is preferable to use as a part of a pipeline.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download