Python:返回ksh函数的输出 [英] Python: return output of ksh function

查看:99
本文介绍了Python:返回ksh函数的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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".

我根据注释建议尝试使用subparser模块.这是我想出的:

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))

我得到一个Permission denied错误.这是回溯:

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

其他详细信息:

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

推荐答案

shlex并未执行您想要的操作:

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.

相反,只需给subprocess.call列出程序名称和所有参数的列表即可:

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)

这篇关于Python:返回ksh函数的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆