简体   繁体   English

如何在python脚本中执行多个CLI命令?

[英]How to execute multiple CLI commands in a python script?

I am using TeamForge's CLI to create artifacts in the Defects tracker section. 我正在使用TeamForge的CLI在“缺陷跟踪器”部分中创建工件。 The CLI file/executable is called "ctf" with no extensions. CLI文件/可执行文件称为“ ctf”,没有扩展名。 I want to use a python script to create artifacts but can only execute one command at most. 我想使用python脚本创建工件,但最多只能执行一个命令。 I want to create a bug in one shot. 我想一次创建一个错误。 This is the code I have so far: 这是我到目前为止的代码:

import os
os.system("./ctf    go tracker1234;             # going to Defects section
                    create;                     # creating an artifact
                    set title This Is A Title;  # setting artifact's fields
                    set description desc123; 
                    set Product [Product 23]; 
                    set build_number Not known; 
                    set Severity Catastrophic; 
                    set steps_to_reproduce 1st comment; 
                    set Component [component 4]; 
                    set Version [version 19]; 
                    commit)                     # saving the artifact on TeamForge

Here is the error I keep getting: 这是我不断得到的错误:

sh: 1: create: not found
sh: 1: commit: not found

So I believe that these commands are not being executed sequentially or in the order that I have specified. 因此,我相信这些命令不会按顺序或按我指定的顺序执行。 This means that each command is being executed separately. 这意味着每个命令都将单独执行。 Any suggestions to get these commands running in the order that I have specified? 有什么建议可以按照我指定的顺序运行这些命令?

Let me know if further explanation is required. 让我知道是否需要进一步说明。

Update 更新

I just found out that you can do this: go tracker1234 create which is two steps in one 我刚刚发现您可以执行以下操作: go tracker1234 create这是两个步骤合而为一的

You could try to quote your arguments to ctf; 您可以尝试引用ctf的参数。 just look at this: 看看这个:

>>> os.system("echo hi; echo again;")
hi
again

versus: 与:

>>> os.system("echo 'hi; echo again;'")
hi; echo again;

The semicolon terminates a command in a shell. 分号可终止外壳程序中的命令。 If your arguments contain semicolons, you must quote them so they don't break your list of arguments. 如果您的参数包含分号,则必须用引号引起来,这样它们才不会破坏您的参数列表。

However, according to the CTF guidelines , their way of handling such a situation with multiple commands seems to be putting them into a script and executing that with: 但是,根据CTF指南 ,他们使用多个命令处理这种情况的方式似乎是将它们放入脚本中并使用以下命令执行:

./ctf script.txt

To pass information to your program on the standard input, either use 要将信息通过标准输入传递给程序,请使用

Alternatively, your program may have some "batch processing mode" that would allow you to provide your set of commands on the command line or in a file, but, looking at the CTFCLI manual page , this doesn't seem likely. 另外,您的程序可能具有某种“批处理模式”,可以让您在命令行或文件中提供一组命令,但是,在CTFCLI手册页上看 ,这似乎不太可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM