如何预填充命令行输入 [英] How to prefill command line input

查看:105
本文介绍了如何预填充命令行输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行bash脚本,我想在执行脚本后用一些命令预填充命令行.唯一的条件是脚本当时不能运行.

I'm running a bash script and I'd like to prefill a command line with some command after executing the script. The only condition is that the script mustn't be running at that time.

我需要的是...

  1. 运行脚本
  2. 脚本停止后,我的命令行中已经预填充了文本

有可能吗?我所尝试的只是使用

Is it even possible? All what I tried is to simulate a bash script using

read -e -i "$comm" -p "[$USER@$HOSTNAME $PWD]$ " input
command $input

但是我正在寻找更简单的方法.

But I'm looking for something more straightforward.

推荐答案

您需要使用TIOCSTI ioctl.这是一个示例C程序,显示了它的工作方式:

You need to use the TIOCSTI ioctl. Here's an example C program that shows how it works:

#include <sys/ioctl.h>

main()
{
    char buf[] = "date";
    int i;
    for (i = 0; i < sizeof buf - 1; i++)
      ioctl(0, TIOCSTI, &buf[i]);
    return 0;
}

编译并运行它,"date"将作为标准输入的缓冲输入,程序退出后您的shell会读取该输入.您可以将其汇总到一个命令中,该命令可以将任何内容填充到输入流中,并在bash脚本中使用该命令.

Compile this and run it and "date" will be buffered as input on stdin, which your shell will read after the program exits. You can roll this up into a command that lets you stuff anything into the input stream and use that command in your bash script.

这篇关于如何预填充命令行输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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