I am trying to execute a shell command kubectl get ns | grep -E '(^|\s)namespace-test($|\s)'
but I am getting empty even if there are errors or not.
Reason I am getting empty for both error and non error cases is it executes the command in two times (commands left and right to |
), But what I need is to get the actual output as a return value, say 0
if there is an output, or 1
if there is no output
This is what I am trying:
get_ns_p1 = subprocess.Popen(['kubectl', 'get', 'ns'], stdout=subprocess.PIPE)
get_ns_p2 = subprocess.Popen(["grep", "-E", "\'(^|\s)"+NAMESPACE+"($|\s)\'"], stdin=get_ns_p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
get_ns_p1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
out_ns, err = get_ns_p2.communicate()
print(out_ns)
can someone help me?
or if you have any approach for this, this is what I want to do...
I want to execute a shell command which has a pipe
and get the output, if there is an output, it should return some value, if the output is empty it should gives a different value, if there is an error
it should gives another different output
How can I achieve this?