简体   繁体   English

如何预填充命令行输入

[英]How to prefill command line input

I'm running a bash script and I'd like to prefill a command line with some command after executing the script. 我正在运行一个bash脚本,我想在执行脚本后用命令行预填充命令行。 The only condition is that the script mustn't be running at that time. 唯一的条件是该脚本当时不能运行。

What I need is to ... 我需要的是......

  1. run the script 运行脚本
  2. have prefilled text in my command line AFTER the script has been stopped 在脚本停止后,在命令行中预填充文本

Is it even possible? 它甚至可能吗? All what I tried is to simulate a bash script using 我尝试的所有内容都是使用模拟bash脚本

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

But I'm looking for something more straightforward. 但我正在寻找更直接的东西。

You need to use the TIOCSTI ioctl. 您需要使用TIOCSTI ioctl。 Here's an example C program that shows how it works: 这是一个示例C程序,它显示了它的工作原理:

#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;
}

Compile this and run it and "date" will be buffered as input on stdin, which your shell will read after the program exits. 编译并运行它,“date”将作为stdin的输入缓冲,shell将在程序退出后读取。 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. 您可以将其汇总到一个命令中,该命令允许您将任何内容填充到输入流中,并在bash脚本中使用该命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM