Skip to content

Commit e8865d3

Browse files
authored
Merge pull request #84 from python-cmd2/pipe_fix
Don't throw exception when piping command output to a shell command
2 parents f1bae95 + b58793b commit e8865d3

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

cmd2.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,11 +1024,17 @@ def restore_output(self, statement):
10241024
if statement.parsed.pipeTo:
10251025
# Pipe output from the command to the shell command via echo
10261026
command_line = r'cat {} | {}'.format(self._temp_filename, statement.parsed.pipeTo)
1027-
result = subprocess.check_output(command_line, shell=True)
1028-
if six.PY3:
1029-
self.stdout.write(result.decode())
1030-
else:
1031-
self.stdout.write(result)
1027+
1028+
# Get the output and display it. Ignore non-zero return codes and print nothing.
1029+
try:
1030+
result = subprocess.check_output(command_line, shell=True)
1031+
if six.PY3:
1032+
self.stdout.write(result.decode())
1033+
else:
1034+
self.stdout.write(result)
1035+
except subprocess.CalledProcessError:
1036+
pass
1037+
10321038
os.remove(self._temp_filename)
10331039
self._temp_filename = None
10341040

0 commit comments

Comments
 (0)