diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-06-29 14:13:18 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-06-29 14:13:18 -0400 |
commit | 37ef113a88e473481f07ec1d2ce99e3abe4ddc30 (patch) | |
tree | 50df7785cf6f78f34af974f37c5f93e6ee427bb0 /cmd2.py | |
parent | 0e1093fc05a45e613192d54da806d924a7f52d95 (diff) | |
download | cmd2-git-37ef113a88e473481f07ec1d2ce99e3abe4ddc30.tar.gz |
Command-line pipe is now handled by subprocess module instead of shell
Instead of using shell functionality to pipe output of a command to a shell command, the Python subprocess is now used to pipe stdout of one process to another.
Diffstat (limited to 'cmd2.py')
-rwxr-xr-x | cmd2.py | 23 |
1 files changed, 11 insertions, 12 deletions
@@ -900,18 +900,17 @@ class Cmd(cmd.Cmd): self.kept_state = None if statement.parsed.pipeTo: - # Pipe output from the command to the shell command via echo - command_line = r'cat {} | {}'.format(self._temp_filename, statement.parsed.pipeTo) - - # Get the output and display it. Ignore non-zero return codes and print nothing. - try: - result = subprocess.check_output(command_line, shell=True) - if six.PY3: - self.stdout.write(result.decode()) - else: - self.stdout.write(result) - except subprocess.CalledProcessError: - pass + # cat the tempfile and pipe the output to the specified shell command + cat_command = 'cat' + p1 = subprocess.Popen([cat_command, self._temp_filename], stdout=subprocess.PIPE) + p2 = subprocess.Popen(shlex.split(statement.parsed.pipeTo), stdin=p1.stdout, stdout=subprocess.PIPE) + p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits. + output, err = p2.communicate() + + if six.PY3: + self.stdout.write(output.decode()) + else: + self.stdout.write(output) os.remove(self._temp_filename) self._temp_filename = None |