0

I am trying to redirect some shell script output to /dev/null within subprocess.call, but it always prints on stdout.

Here is my shell script

# cat /usr/local/scripts/test_unknown.sh
#!/bin/bash

echo "Testing Unknown..."
exit 3

Executing in python

# python
Python 2.7.5 (default, Aug  4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(["/bin/sh", "/usr/local/scripts/test_unknown.sh", "2>&1 >/dev/null"])
Testing Unknown...
3
>>>

Executing the same script in command line

# /bin/sh "/usr/local/scripts/test_unknown.sh" 2>&1 >/dev/null
#

What am i missing ?

Ronin Goda
  • 234
  • 5
  • 13
  • is there no other way (other than subprocess) which i can use for executing the script and redirect the output to /dev/null :(, eventually all i need is the return value of the script. – Ronin Goda Apr 23 '18 at 12:14
  • Since you only want the return code, you _could_ run the script using `os.system()`. That would let you embed the shell redirection into the command string. But it's probably better to use `subprocess` and to use the proper args to tell it to send the output to devnull, as shown in the linked question. – PM 2Ring Apr 23 '18 at 12:25

0 Answers0