summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <tleonhardt@gmail.com>2017-04-04 16:59:28 -0400
committerTodd Leonhardt <tleonhardt@gmail.com>2017-04-04 16:59:28 -0400
commitb58793bf233c8ff59f12d48e72bb9849a8e0d9bd (patch)
treec63dcfb9eff2561e24fca75b955da5d5b7a41bcc
parentf1bae9593c2fb240c77f4b4df809a10093f3f948 (diff)
downloadcmd2-git-b58793bf233c8ff59f12d48e72bb9849a8e0d9bd.tar.gz
Don't throw exception when piping command output to a shell command
If a non-zero return code is returned from the shell command, just ignore it.
-rwxr-xr-xcmd2.py16
1 files changed, 11 insertions, 5 deletions
diff --git a/cmd2.py b/cmd2.py
index 4dcd9ba7..4de4a097 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1024,11 +1024,17 @@ class Cmd(cmd.Cmd):
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)
- result = subprocess.check_output(command_line, shell=True)
- if six.PY3:
- self.stdout.write(result.decode())
- else:
- self.stdout.write(result)
+
+ # 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
+
os.remove(self._temp_filename)
self._temp_filename = None