Skip to content

Instantly share code, notes, and snippets.

@gameame
Created February 21, 2011 14:49
Show Gist options
  • Save gameame/837157 to your computer and use it in GitHub Desktop.
Save gameame/837157 to your computer and use it in GitHub Desktop.
Execute a command on each line in input using the line as parameter
#!/usr/bin/env python
# Example usage:
# ls|each cd PARAM\;pwd
import sys, subprocess
command = " ".join(sys.argv[1:])
command = command.replace("PARAM", "%(p)s")
i = 0
for parameter in sys.stdin.readlines():
c = command % {'p': parameter.strip()}
print "***", c
p = subprocess.Popen(c, shell=True, executable="/bin/bash")
p.wait()
i += 1
print "Executed %d times" % i
@gameame
Copy link
Author

gameame commented Feb 22, 2011

Equivalente a:

find . -maxdepth 1 -type d -exec sh -c "cd {}; pwd"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment