如何让 readline 在终端启动时添加预先“键入”的文本?

gla*_*rry 6 shell terminal readline

通过预先“输入”,我的意思是交互式控制台有代码文本等待用户通过简单地按 Enter 键来(编辑和)运行。

看起来 readline 应该支持某些东西,但确认它不够好。至少我会知道安装额外的自动化工具(如expect)是唯一的方法。

Run*_*ium 3

不确定这是否有帮助,并且不是每个 readline,但如果 python 是替代方案(或类似的),则一种方法可能是:

#!/usr/bin/env python
""" Inject command to own command line """

import sys, fcntl, termios

def main():
    """ x """
    tty = sys.stdin
    old_attr = termios.tcgetattr(tty)
    new_attr = termios.tcgetattr(tty)
    # No echo please
    new_attr[3] &= ~termios.ECHO
    termios.tcsetattr(tty, termios.TCSANOW, new_attr)

    cmd = ' '.join(sys.argv[1:])
    for char in cmd:
        fcntl.ioctl(tty, termios.TIOCSTI, char)

    termios.tcsetattr(tty, termios.TCSANOW, old_attr)

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

如:

 script_name command to inject
Run Code Online (Sandbox Code Playgroud)