Python scripting with xargs

What will be the process of writing Python scripts using "xargs"? For example, I would like the following command to work through each line of a text file and execute an arbitrary command:

cat servers.txt | ./hardware.py -m 

Essentially, each line should be passed to a hardware.py script.

+5
source share
3 answers

For your teams to work with xargs, you just need them to accept the arguments. The arguments in Python are listed sys.argv. This way you can do somthing like:

find . -type f -name '*.txt' -print0 | xargs -0 ./myscript.py

which may be equivalent

./myscript.py ./foo.txt ./biz/foobar.txt ./baz/yougettheidea.txt

, sys, sys.stdin, . , :

./myscript.py < somefile.txt
+6

.

-, stdin. xargs. , hardware.py, sys.stdin , :

if __name__=='__main__':
    for line in sys.stdin:
         do_something(line)

, hardware.py , , , sys.stdout

-, . xargs. :

xargs./hardware.py < servers.txt # , cat servers.txt | xargs./hardware.py

"" server.txt( ) hardware.py(, ). , hardware.py word1 word2 word3 word4 ...

Python sys.arvg. sys.argv[0] , sys.argv[1:] . , , argparse.

+1

, . ./hardware.py -m , GNU Parallel :

cat servers.txt | parallel --pipe -N1 ./hardware.py -m

./hardware.py -m , :

cat servers.txt | parallel ./hardware.py -m

GNU Parallel :

wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
chmod 755 parallel
cp parallel sem

Watch videos for GNU. Learn more at the same time: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

0
source

All Articles