Python: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 returned
错误。这是回溯:
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
shlex 没有执行您想要的操作:
您正在尝试执行根目录,但这是不允许的,因为它是一个目录而不是可执行文件。
相反,只需为 subprocess.call 提供程序名称和所有参数的列表:
shlex
is not doing what you want: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: