如何将命令作为在 python 子进程内启动的 shell 的输入

发布于 2024-09-25 03:02:12 字数 490 浏览 4 评论 0原文

我想创建一个 GUI python 脚本来启动多个进程。所有这些进程最初都是通过用perl脚本(start_workspace.perl)设置一个shell来调用的,并在shell下键入可执行文件名。 在start_workspace.perl里面,它首先设置一些ENV变量,然后调用exec(/bin/bash),它启动shell,所以你可以在提示符下输入“execfile”来启动。

我的问题是,从我的 python 脚本中,我仍然想使用这个 shell(通过 subprocess.popen("perl start_workspace.perl")),但我不想停止手动输入“execfile”。我希望我可以在调用“start_workspace.perl”的步骤中指定“execfile”,并且该过程可以在没有任何干预的情况下完成。

像命令重定向之类的东西。但我不知道是否可能。

subprocess.popen(("perl start_workspace.perl") < "execfile")

I want to create a GUI python script to launch several processes. All of these processes originally were called by setting up a shell with a perl script (start_workspace.perl), and type the executable file name under the shell.
inside, start_workspace.perl, it first set some ENV variables, and then call exec(/bin/bash), which launch the shell, so you can type "execfile" under the prompt to launch.

my problem, from my python script, I still want to use this shell (by subprocess.popen("perl start_workspace.perl")), but I do not want to be stopped to manually input "execfile". I want someway that I can specify the "execfile" at step of calling "start_workspace.perl", and the process can completed without any intervention.

something like command redirection. but I do not know if it is possible.

subprocess.popen(("perl start_workspace.perl") < "execfile")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一指流沙 2024-10-02 03:02:12

你们非常接近。在子流程文档中,请参阅:

stdinstdoutstderr分别指定执行程序的标准输入、标准输出和标准错误文件句柄。有效值为 PIPE、现有文件描述符(正整数)、现有文件对象和 None。 PIPE 指示应创建一个通往子级的新管道。使用 None 则不会发生重定向;子级的文件句柄将从父级继承。此外,stderr 可以是 STDOUT,这表示来自应用程序的 stderr 数据应捕获到与 stdout 相同的文件句柄中。

stdin、stderr 和 stdout 是 popen 方法的命名参数。您可以打开输入文件并将其文件描述符作为标准输入传递给新进程。

You are very close. In the subprocess documentation, see:

stdin, stdout and stderr specify the executed programs’ standard input, standard output and standard error file handles, respectively. Valid values are PIPE, an existing file descriptor (a positive integer), an existing file object, and None. PIPE indicates that a new pipe to the child should be created. With None, no redirection will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can be STDOUT, which indicates that the stderr data from the applications should be captured into the same file handle as for stdout.

stdin, stderr, and stdout are named parameters for the popen method. You can open the input file and pass its file descriptor as stdin to your new process.

情深如许 2024-10-02 03:02:12

使用subprocess模块,可以通过这种方式实现。您可以使用标准输入流编写命令,以便在设置实际环境后执行。

start_workspace.perl

print "Perl: Setting some env variables\n";
$ENV{"SOME_VAR"} = "some value";
print "Perl: Starting bash\n";
exec('bash');

在 python 中:

import subprocess 
p = subprocess.Popen( "perl start_workspace.perl", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write('echo "Python: $SOME_VAR"\n')
p.stdin.write("make\n")
(stdoutdata, stderrdata) = p.communicate()
print stdoutdata
print stderrdata

输出是

Perl: Setting some env variables
Perl: Starting bash
Python: some value

make: *** No targets specified and no makefile found.  Stop.

Using the subprocess module, it could be achieved this way. You can use the stdin stream to write your command to execute once the actual environment has been set.

start_workspace.perl

print "Perl: Setting some env variables\n";
$ENV{"SOME_VAR"} = "some value";
print "Perl: Starting bash\n";
exec('bash');

In python:

import subprocess 
p = subprocess.Popen( "perl start_workspace.perl", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write('echo "Python: $SOME_VAR"\n')
p.stdin.write("make\n")
(stdoutdata, stderrdata) = p.communicate()
print stdoutdata
print stderrdata

Output is

Perl: Setting some env variables
Perl: Starting bash
Python: some value

make: *** No targets specified and no makefile found.  Stop.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文