【发布时间】:2020-09-18 09:32:23
【问题描述】:
我在 suse linux 中运行这个 python 函数来从 /etc/hosts grep 节点的 ip--
def mm_node():
import os
node_name = os.system("`cat /etc/hosts | egrep -i mm | grep om | awk '{print $1}'`")
return node_name
mm_node()
结果,它显示了这个奇怪的输出
sh: 192.168.10.10: command not found
而不是
192.168.10.10
如果我运行 shell 命令
(cat /etc/hosts | egrep -i mm | grep om | awk '{print $1}')
直接在linux命令提示符下,它给出的o/p as
192.168.10.10
【问题讨论】:
-
从系统字符串命令中删除反引号:
"cat /etc/hosts | egrep -i mm | grep om | awk '{print $1}'" -
为什么一开始这里有反引号?严肃的问题;我不认为我们有一个规范的实例来关闭这个常见的重复错误,但我们真的应该这样做。不过,我无法写出令人信服的例子,因为我首先不理解为什么有人会编写这段代码,而且我从未见过任何解释。