Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prefill command line input

Tags:

linux

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.

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

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

But I'm looking for something more straightforward.

like image 903
Jan Vorcak Avatar asked Jun 02 '12 21:06

Jan Vorcak


Video Answer


1 Answers

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

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.

like image 59
Kyle Jones Avatar answered Sep 24 '22 12:09

Kyle Jones