prova.sh 包含:
#!/bin/bash
echo "Output that I don't want."
echo "Output that I don't want."
echo "Output that I don't want."
echo -e "Output that I want.\nI want this too.\
\nI want this too." #This is the last command of the bash script, which is what I'm looking for.
这个解决方案:
import subprocess
output = subprocess.check_output('./prova.sh', shell=True, text=True)
print(output, end='')
将所有shell命令的标准输出放入一个变量中:
Output that I don't want.
Output that I don't want.
Output that I don't want.
Output that I want.
I want this too.
I want this too.
但我只想要最后一个shell命令的标准输出:
Output that I want.
I want this too.
I want this too.
我该如何获取它?
Python 3.8.5
现有的问题只涉及如何获取N行或类似内容。与此相反,我只想要上一个命令的输出。