summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py23
1 files changed, 11 insertions, 12 deletions
diff --git a/cmd2.py b/cmd2.py
index 0c6c2c1a..10b12516 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -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