I have a script part that looks like this:
if ( pstree -s $$ | grep -q 'sshd' ); then
SOME_VAR=foo
else
SOME_VAR=boo
fi
I need to do the same thing in python. Is there a way that I can get bool output from the command inside if()? Maybe using the exit code or smth, so the code looks something like this:
if os.system("pstree -s $$ | grep -q 'sshd'"):
os.environ['SOME_VAR']='foo'
else:
os.environ['SOME_VAR']='boo'
I tried to use subprocess
library like this:
args = shlex.split("pstree -s $$ | grep -q 'sshd'")
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
o, e = p.communicate()
print('Output: ' + o.decode('ascii'))
print('Error: ' + e.decode('ascii'))
print('code: ' + str(p.returncode))
but it says Error: pstree: invalid option -- 'q'