如何在Python subprocess.Popen中使用awk?
下面是我的Python代码,我需要用awk来查找在k8s pod中运行的某个进程(SWeNP)的编号。
cs_swo_cmd = f"oc exec -it {pod_name} bash -c cs-container -- ps -eaf | grep -i 'SWe_NP' | awk \'{print $2}\'"
swo_process = subprocess.Popen(cs_swo_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
但是当我运行这段代码时,出现了下面的错误,请帮帮我。
stella@bts16:~$ python3 k8sSwitchover.py
File "k8sSwitchover.py", line 70
cs_swo_cmd = f"oc exec -it $i bash -c cs-container -- ps -eaf | grep -i 'SWe_NP' | awk {'print $2}"
^
SyntaxError: f-string: unterminated string
stella@bts16:~$
1 个回答
1
试试这个替代方案来使用 cw_swo_cmd
:
cs_swo_cmd = f"oc exec -it {pod_name} bash -c cs-container -- ps -eaf | grep -i 'SWe_NP' | awk '{{print $2}}'"
这样应该能得到正确的结果:
"oc exec -it $i bash -c cs-container -- ps -eaf | grep -i 'SWe_NP' | awk '{print $2}'"