Python:ksh函数的返回输出

发布于 2024-12-06 20:26:24 字数 1005 浏览 0 评论 0原文

在 Unix 上,如何将 ksh 函数的输出作为 Python 变量检索? 该函数称为 sset 并在我的“.kshrc”中定义。

我根据评论建议尝试使用 subparser 模块。这是我的想法:

import shlex
import subprocess

command_line = "/bin/ksh -c \". /Home/user/.khsrc && sset \""
s = shlex.shlex(command_line)

subprocess.call(list(s))

我收到一个 Permission returned 错误。这是回溯:

Traceback (most recent call last):
  File "./pymss_os.py", line 9, in <module>
    subprocess.call(list(s))
  File "/Soft/summit/tools/Python-2.7.2/Lib/subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/Soft/summit/tools/Python-2.7.2/Lib/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/Soft/summit/tools/Python-2.7.2/Lib/subprocess.py", line 1228, in _execute_child
    raise child_exception
OSError: [Errno 13] Permission denied

额外详细信息:

  • Python 2.7
  • Ksh Version M-11/16/88i
  • Solaris 10 (SunOS 5.10)

On Unix, how can Iretrieve the output of a ksh function as a Python variable?
The function is called sset and is defined in my ".kshrc".

I tried using the subparser module according to comment recommendations. Here's what I came up with:

import shlex
import subprocess

command_line = "/bin/ksh -c \". /Home/user/.khsrc && sset \""
s = shlex.shlex(command_line)

subprocess.call(list(s))

And I get a Permission denied error. Here's the traceback:

Traceback (most recent call last):
  File "./pymss_os.py", line 9, in <module>
    subprocess.call(list(s))
  File "/Soft/summit/tools/Python-2.7.2/Lib/subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/Soft/summit/tools/Python-2.7.2/Lib/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/Soft/summit/tools/Python-2.7.2/Lib/subprocess.py", line 1228, in _execute_child
    raise child_exception
OSError: [Errno 13] Permission denied

Extra details:

  • Python 2.7
  • Ksh Version M-11/16/88i
  • Solaris 10 (SunOS 5.10)

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

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

发布评论

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

评论(1

故事未完 2024-12-13 20:26:24

shlex 没有执行您想要的操作:

>>> list(shlex.shlex("/bin/ksh -c \". /Home/user/.khsrc\""))
['/', 'bin', '/', 'ksh', '-', 'c', '". /Home/user/.khsrc"']

您正在尝试执行根目录,但这是不允许的,因为它是一个目录而不是可执行文件。

相反,只需为 subprocess.call 提供程序名称和所有参数的列表:

import subprocess

command_line = ["/bin/ksh", "-c", "/Home/user/.khsrc"]
subprocess.call(command_line)

shlex is not doing what you want:

>>> list(shlex.shlex("/bin/ksh -c \". /Home/user/.khsrc\""))
['/', 'bin', '/', 'ksh', '-', 'c', '". /Home/user/.khsrc"']

You're trying to execute the root directory, and that is not allowed, since, well, it's a directory and not an executable.

Instead, just give subprocess.call a list of the program's name and all arguments:

import subprocess

command_line = ["/bin/ksh", "-c", "/Home/user/.khsrc"]
subprocess.call(command_line)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文