Bash – xargs Loop with Input Variable for Multi-Command Use

bashxargs

I have a text file with various IP addresses.

I would like to cycle through each, and issue a command:

cat ips.txt | xargs -L 1 `ssh me@__IP__ echo "text" > file; reboot;`

But I'm unsure how to pass the xargs value to _IP_.

Could someone explain how this could be accomplished?

Best Answer

You don't need either cat or xargs for this: a simple read in a loop would suffice:

while read -r IP;do
    ssh me@$IP echo "text" > file
    reboot
done < ips.txt

For future reference, what you want for xargs can be achieved with the -I option: you supply a name after -I and any instances of that name in the command itself will be replaced by the arguments xargs receives on the pipe:

... | xargs -L 1 -I myip ssh me@myip 'echo "text">file;reboot'
Related Question