Victor
Victor

Reputation: 447

How to pipe an output of first command to go to specific location of next command?

I am running a command that spits out IPs, I need to feed it to another program at a specific location as it comes, how do I do it?

$ command1 | command2  -c configfile -i "$1" status

"$1" is where I want the result of command1 to go to.

Thanks.

Upvotes: 2

Views: 2732

Answers (3)

karakfa
karakfa

Reputation: 67567

xargs is your tool

$ command1 | xargs -I {} command2 -c configfile -i {} status

you can refer to the argument multiple times, for example

$ echo this | xargs -I {} echo {}, {}, and {}

this, this, and this

based on the last comment, perhaps you want to do something like this

$ var=$(command1) && command2 "$var" ... | command3 "$var" ...

Upvotes: 5

WalterG
WalterG

Reputation: 1

There are probably 100 ways to do this in a bash shell. This one is quick and easy. We can use ifconfig, grep the IP address, use awk to pull it out and assign it to an environment variable. Then use the boolean operator to run the next command which uses the environment variable

IPADDR=`ifconfig | grep 172 | awk '{print $2}' | awk -F: '{print $2}'` && echo $IPADDR

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295815

To pass command2 a filename which will, when read, provide output from command1, the appropriate tool is process substitution:

command2 -c configfile -i <(command1) status

The <(...) syntax will be replaced with a filename -- on Linux, of the form /dev/fd/NN; on some other platforms a named pipe instead -- from which the output of command2 can be streamed.

Upvotes: 1

Related Questions