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 xargs
you simply need them to accept arguments. Arguments in Python are in the sys.argv
list. In this way you can execute somthing like:
find . -type f -name '*.txt' -print0 | xargs -0 ./myscript.py
which might be equivalent to
./myscript.py ./foo.txt ./biz/foobar.txt ./baz/yougettheidea.txt
To make your commands work with standard input, you can also use the sys
module, this time with sys.stdin
, which you can treat as a file. This is more like the example you gave:
./myscript.py < somefile.txt