如何从Jython执行交互式shell? [英] How can I exec an interactive shell from Jython?

查看:215
本文介绍了如何从Jython执行交互式shell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些示例Jython或Java代码来执行交互式OS shell(/ bin / sh)。我们的开发环境是Linux上的Java。我想要做的是为开发人员提供一种编写Jython脚本的方法,然后放入一个shell,它允许他们检查系统。我需要从Jython或Java执行此操作的原因 - 而不是将所有这些包装在shell脚本中 - 是我们使用Java加载一些库,我需要在交互式shell运行时保持加载。如果JVM在交互式shell运行之前终止,我们的一些自定义硬件将被重置。

I need some example Jython, or Java, code to exec an interactive OS shell (/bin/sh). Our development environment is Java on Linux. What I would like to do is provide a means for developers to write Jython scripts, and then drop into a shell, which allows them to inspect the system. The reason I need to exec this from Jython or Java--as opposed to just wrapping all this in a shell script--is that we use Java load some libraries which I need to keep loaded while the interactive shell is running. If the JVM were to terminate before the interactive shell runs, some of our custom hardware would be reset.

我查看了Jython子进程模块。但是,我所看到的大部分内容只会在子进程上运行并等待终止(即非交互式)。这些示例都没有显示如何以交互方式调用shell。我见过的大多数Java java.lang.Runtime.exec()示例也是如此。

I have looked at the Jython subprocess module. However, most of what I have seen will only run and wait on a subrocess to terminate (ie non-interactive). None of the examples show how to the shell can be invoked interactively. The same is true of most of the Java java.lang.Runtime.exec() examples I've seen.

推荐答案

什么这个简单的jython代码是错误的,是stdin,stdout不能正常工作

What is wrong with this simple jython code, is that the stdin, stdout do not work as we expect

import subprocess
import os          
subprocess.call([os.environ.get('SHELL', '/bin/sh'), '-i'])

假设我们想在tty上生成shell,以下似乎
给出更好的结果:

Assuming that we want to spawn the shell on the tty, the following seems to give better results:

import os
import subprocess

subprocess.call([os.environ.get('SHELL', '/bin/sh') +
    ' -i < /dev/tty > /dev/tty 2>&1'], shell=True)

这将在另一个子shell中运行shell,用活动tty替换残缺的stdin,stdout流。

This will run the shell within another subshell, that replaces the crippled stdin, stdout streams with the active tty.

这篇关于如何从Jython执行交互式shell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆