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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With