开发者

Python: return output of ksh function

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)


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜