How to return to bash prompt after printing output from backgrounded function?

后端 未结 3 467
清酒与你
清酒与你 2020-12-24 15:20

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

相关标签:
3条回答
  • 2020-12-24 15:52

    What is the problem you're trying to solve?

    Right now, this is more or less a cosmetic problem. You're still in the shell, and the prompt is still there. Just type another command and it will be executed.

    Alternatively, run the function in the foreground, or if you need to do something else in between, use wait:

    $ fn & pid=$!
    $ : something else
    $ wait ${pid}
    
    0 讨论(0)
  • 2020-12-24 16:02

    similar to Henk Langevelds solution.

    Find the pid of your script
    wait for it to finish.
    echo a line
    Prompt is back
    Unfortunately you're going to get that blank line still

    #!/bin/bash
    
    fn(){
        sleep 1
        echo "Done"
    
    }
    fn &
    PID=$!
    wait $PID
    echo -e ''
    
    0 讨论(0)
  • 2020-12-24 16:06

    Compile below code to file a.out

    #include <sys/ioctl.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    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.

    0 讨论(0)
提交回复
热议问题