View Single Post
  #5   (View Single Post)  
Old 25th March 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Default

more or less yeah, that's the example case I was using ephemera.

As to your example, except if one doesn't want the parent to pop a cork when the sub shell closes, something like this might be more appropriate:

Code:
ALIVE=1
command="sleep 5"       # lengthy processing program
trap 'ALIVE=0' TERM
printf "running \"$command\" ";
($command; printf " finished with exitcode=$?\n"; kill $$)&
while [ $ALIVE -gt 0 ]; do printf .; sleep 1; done
echo continue with other work when child is done


you could say, the idea of a fork built in would be to say something like, "setup the trap for me"


or to rewrite the original (fork) example with trap:

Code:
# a super simple but conceptual example

# a variable to communicate status between child/parent
ALIVE=1
trap 'ALIVE=0' TERM

if [ $? -ne 0 ]; then
    # Note: if trap ever errors out beyond syntax errors
    #     I never check the exit status of trap in my
           own scripts
    echo "$0: Unable to trap SIGTERM for ..., stopping $0"
    exit 255
else
    echo "doing processing: ... whatever"

    (do lengthy processing in child; kill $$) &

    # when the child exits, the variable becomes false (and exits the loop)
    while [ $ALIVE -gt 0 ]; do
        printf '.'; sleep 1
    done
    echo 'done processing'
fi


(this reminds me, making key bindings in ksh93 is a fun)
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote