Reputation: 19989
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 ...
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.
Upvotes: 9
Views: 2001
Reputation: 5532
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.
Upvotes: 11