1

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)
rahmu
  • 5,708
  • 9
  • 39
  • 57
  • 3
    Search. Here's a hint. Search here for "subprocess" and "collect output" and "python". – S.Lott Sep 27 '11 at 10:06
  • possible duplicate of [How to call external command in Python](http://stackoverflow.com/questions/89228/how-to-call-external-command-in-python) – S.Lott Sep 27 '11 at 10:07
  • http://stackoverflow.com/search?q=%5Bpython%5D+shell+pipe – sehe Sep 27 '11 at 10:29

1 Answers1

3

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)
rahmu
  • 5,708
  • 9
  • 39
  • 57
phihag
  • 278,196
  • 72
  • 453
  • 469