Last active
October 2, 2017 13:00
-
-
Save GaryLee/d4035d13a26e5a3b9f453e7a3e9fba51 to your computer and use it in GitHub Desktop.
Execute shell command in Perl-like manner.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
from subprocess import CalledProcessError, check_output | |
def cmd(cmd_arg): | |
"""Execute shell command in Perl-like manner.""" | |
ctx = globals().copy() | |
ctx.update(sys._getframe(1).f_locals) | |
for ln in check_output(cmd_arg.format(**ctx), shell=True).split(os.linesep): | |
yield ln | |
filelist = '*.txt' | |
pattern = 'xyz' | |
for ln in cmd('dir {pattern} {filelist}'): | |
print ln | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment