What would be the process of making my Python scripts work well with ‘xargs’? For instance, I would like the following command to work through each line of text file, and execute an arbitrary command:
cat servers.txt | ./hardware.py -m
Essentially would like each line to be passed to the hardware.py script.
To make your commands work with
xargsyou simply need them to accept arguments. Arguments in Python are in thesys.argvlist. In this way you can execute somthing like:which might be equivalent to
To make your commands work with standard input, you can also use the
sysmodule, this time withsys.stdin, which you can treat as a file. This is more like the example you gave: