Python:返回ksh函数的输出

时间:2011-09-27 10:04:28

标签: python unix exec ksh

在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 denied错误。这是追溯:

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)

1 个答案:

答案 0 :(得分:3)

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)