How can I return to my bash prompt automatically after printing output from a function that was put in the background?
For example, when I run the following script i
Compile below code to file a.out
#include
#include
#include
int main(int argc, char *argv[])
{
/* char buf[] = "date\n"; */
char buf[] = "\n"; /* Data to write on terminal */
int i;
int fd = open(argv[1], O_WRONLY); /* Open terminal */
/* printf("fd = %d\n", fd); */
for (i = 0; i < sizeof buf - 1; i++) /* Write data */
ioctl(fd, TIOCSTI, &buf[i]);
close(fd); /* Close file descriptor */
return 0;
}
This program expects a path as command line argument. Program will open the path and write a new line to this path.
If this path happen to contain the file descriptor of a writable terminal running bash script, this would cause bash to catch a new prompt.
Modify your shell script
fn(){
sleep 10
echo "Done"
./a.out /proc/$PPID/fd/0
}
fn &
This script would do some job (represented with sleep here) and then call the utility written previously with argument as input terminal of parent. Parent terminal would receive a new line and catch a new prompt discarding the stray command on this prompt if any.
/proc
contains directories for all processes. Name of folder matches to the pid of process.
Inbuild variable PPID
contains the parent's pid.
Inside the pid
directory, there is an fd
directory containing open streams.
0
is for input, 1
is for output and 2
is for error. There may be more open streams depending on the process. We are interested in 0
stream here.