Search code examples
linuxbashshellnamed-pipes

Bash non blocking source (background???)


im trying to get a bash to pass argument to another bash, I manged to do this by sourcing one of the and calling it from the other so they would be parent and child under the same shell. the problem is that the child is a loop becuase im traying to catch events (it might be reading a fifo(pipefile)) and this blocked the bash... right, so i tought hmmm let's try running the bash on the back ground and the problem i get here i guess is that the variables dont come int becuase i guess it is not under the same shell i tried sourcing and backgounding that but no possitive results...

also at some point with testing my shell starts going crazy when i send to fifo(pipe file) even if nothing (that i know is reading) it acts as if something had read that fifo, and some of my other bash screens start doing crazy stuff... :O

anyway was wondering if anyone can help me. Dont know if what im trying to do is even possible... Thanks.

DOnt know where i read (haven been able to find the page again, yet) that moveing script to the background makes it accesible to parent and child, subprocess and its the papas mamas, but that's not my experience up to now.

bash1

   #!/bin/bash

    while true
    do

    . ./bash2.sh &

    if [[ $VARIABLE = 1 ]]; then
    echo "message recevied"
    fi

bash2

  #!/bin/bash

    while true
    do

    if [[ $(cat ./fifo1) = 0 ]]; then
    VARIABLE=1
    fi
    done

Thanks


Solution

  • Because & always starts a new process, there is little difference between

    . ./bash2.sh &
    

    and

    # Assuming bash2.sh is executable
    ./bash2.sh &
    

    Any variables defined by bash2.sh are confined to the process that executes it; they are not available to bash1.sh. Your choices are

    1. Run . ./bash2.sh in the foreground, and wait for it to complete
    2. Have ./bash2.sh write to something (a file, named pipe, etc) which bash1.sh can read from.