调用内部循环检查输出

2024-07-08 14:46:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用check\u output向shell传递一个命令,其中包含for循环: 例如:

 check_output("ceph osd erasure-code-profile set LRCprofile /\ "
      + "plugin=lrc /\ \n"
      + "layers='[ \n"
      for i in range (0,layers):
          + " str(''.join(lay[i])) \n "
      + "]' "
      + , shell=True)

所以命令的形式是:

ceph osd erasure-code-profile set LRCprofile \
     plugin=lrc \
     layers='[
               [ "_cDD_cDD", "" ],
               [ "cDDD____", "" ],
               [ "____cDDD", "" ],
             ]'

这是一个错误。你知道吗

有没有一种方法可以根据参数向shell传递大小可变的命令?你知道吗

谢谢


Tags: 命令foroutputlayerscheckcodeshellprofile
2条回答

以下假定为Python 3:

layers = [
    "_cDD_cDD",
    "cDDD____",
    "____cDDD"
]

layers = [e for layer in layers for e in (layer, "")]
layers = str(layers).replace("'", '"')

command = [
    "ceph", "osd",
    "erasure-code-profile", "set", "LRCprofile",
    "plugins=lrc",
    "layers='{}'".format(layers)
]

使用print(" ".join(command))打印命令返回:

ceph osd erasure-code-profile set LRCprofile plugins=lrc layers='["_cDD_cDD", "", "cDDD____", "", "____cDDD", ""]'

您应该能够将列表变量command直接与check_output一起使用。你知道吗

check\u output get list作为参数,而不是字符串。所以您可以尝试在单独的步骤中生成命令,将其转换为list,然后在check\u输出中将此list用作参数

相关问题 更多 >

    热门问题