如何在python中的变量中存储执行的shell命令的结果?

时间:2021-09-23 15:44:05

I need to store the result of a shell command that I executed in a variable. But i couldn get that. I tried like:

我需要存储我在变量中执行的shell命令的结果。但我无法做到这一点。我尝试过:

call=os.system("cat syscall_list.txt | grep f89e7000 | awk '{print $2}'")
print call

But it prints the result in terminal and prints the value of call as zero, possibly indicating as success. How to get the result stored in a variable?

但它将结果打印在终端中并将call的值打印为零,可能表示成功。如何将结果存储在变量中?

3 个解决方案

#1


31  

Use the subprocess module instead:

改为使用子进程模块:

import subprocess
output = subprocess.check_output("cat syscall_list.txt | grep f89e7000 | awk '{print $2}'", shell=True)

Edit: this is new in Python 2.7. In earlier versions this should work (with the command rewritten as shown below):

编辑:这是Python 2.7中的新功能。在早期版本中,这应该工作(使用如下所示的命令重写):

import subprocess
output = subprocess.Popen(['awk', '/f89e7000/ {print $2}', 'syscall_list.txt'], stdout=subprocess.PIPE).communicate()[0]

As a side note, you can rewrite

作为旁注,您可以重写

cat syscall_list.txt | grep f89e7000

To

grep f89e7000 syscall_list.txt

And you can even replace the entire statement with a single awk script:

你甚至可以用一个awk脚本替换整个语句:

awk '/f89e7000/ {print $2}' syscall_list.txt

Leading to:

导致:

import subprocess
output = subprocess.check_output(['awk', '/f89e7000/ {print $2}', 'syscall_list.txt'])

#2


9  

commands.getstatusoutput would work well for this situation.

commands.getstatusoutput适用于这种情况。

import commands
print(commands.getstatusoutput("cat syscall_list.txt | grep f89e7000 | awk '{print $2}'"))

#3


0  

All the other answers here are fine answers. In many situations, you do need to run external commands.

这里的所有其他答案都是很好的答案。在许多情况下,您需要运行外部命令。

This specific example has another option: you can read the file, process it line by line, and do something with the output.

此特定示例还有另一个选项:您可以读取文件,逐行处理,并对输出执行某些操作。

While this answer doesn't work for the "more general question being asked", I think it should always be considered. It is not always the "right answer", even where possible. Remembering this (easier), and knowing when (not) to apply it (more difficult), will make you a better programmer.

虽然这个答案对“被问到的更普遍的问题”不起作用,但我认为应该始终予以考虑。即使在可能的情况下,它也并非总是“正确答案”。记住这个(更容易),知道何时(不)应用它(更难),将使你成为一个更好的程序员。

#1


31  

Use the subprocess module instead:

改为使用子进程模块:

import subprocess
output = subprocess.check_output("cat syscall_list.txt | grep f89e7000 | awk '{print $2}'", shell=True)

Edit: this is new in Python 2.7. In earlier versions this should work (with the command rewritten as shown below):

编辑:这是Python 2.7中的新功能。在早期版本中,这应该工作(使用如下所示的命令重写):

import subprocess
output = subprocess.Popen(['awk', '/f89e7000/ {print $2}', 'syscall_list.txt'], stdout=subprocess.PIPE).communicate()[0]

As a side note, you can rewrite

作为旁注,您可以重写

cat syscall_list.txt | grep f89e7000

To

grep f89e7000 syscall_list.txt

And you can even replace the entire statement with a single awk script:

你甚至可以用一个awk脚本替换整个语句:

awk '/f89e7000/ {print $2}' syscall_list.txt

Leading to:

导致:

import subprocess
output = subprocess.check_output(['awk', '/f89e7000/ {print $2}', 'syscall_list.txt'])

#2


9  

commands.getstatusoutput would work well for this situation.

commands.getstatusoutput适用于这种情况。

import commands
print(commands.getstatusoutput("cat syscall_list.txt | grep f89e7000 | awk '{print $2}'"))

#3


0  

All the other answers here are fine answers. In many situations, you do need to run external commands.

这里的所有其他答案都是很好的答案。在许多情况下,您需要运行外部命令。

This specific example has another option: you can read the file, process it line by line, and do something with the output.

此特定示例还有另一个选项:您可以读取文件,逐行处理,并对输出执行某些操作。

While this answer doesn't work for the "more general question being asked", I think it should always be considered. It is not always the "right answer", even where possible. Remembering this (easier), and knowing when (not) to apply it (more difficult), will make you a better programmer.

虽然这个答案对“被问到的更普遍的问题”不起作用,但我认为应该始终予以考虑。即使在可能的情况下,它也并非总是“正确答案”。记住这个(更容易),知道何时(不)应用它(更难),将使你成为一个更好的程序员。