简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM