exit value (MORE INFO)

Casper H.S. Dik casper at fwi.uva.nl
Thu Nov 15 01:30:42 AEST 1990


stadt at cs.utwente.nl (Richard van de Stadt) writes:

>With this posting I want to explain with an example why I want to know
>the exit value of a process that was started in background. From reactions
>of some of you (thank you very much) I understand that the question was a
>bit vague.

>First of all I'm not a newcomer to UNIX (know it since 1983). 
>I didn't ask how to get an exit value of a process, because I already 
>know that's in $?. I also know that the process id of the last process
>started in background is in $!.

In that case you know enough:

     wait [ n ]     Wait for the background process whose process
                    ID  is  n  and report its termination status.
                    If n is omitted, all  the  shell's  currently
                    active  background  processes  are waited for
                    and the return code will be zero.

The solution would be:
	job &
	proc=$!
	rest ...
	wait $proc
	if [ $? = 1 ]; then ... job failed ... ; fi

Or in your script:
------------------------------------------------------------------------
#! /bin/sh
#name of script: floppyformat

doit () {   # a function
attent /dev/dk1    #find out if -m option must be used (new floppy)
  case $? in
    0) # not a new floppy
       dkformat /vol/name_of_floppy >/dev/null 2>&1  #no output on screen
       exitval=$?
       break ;;
    *) # a new floppy
       dkformat -m /vol/new_name >/dev/null 2>&1 &   #no output on screen
	proc=$!
       sleep 10 # be sure process is started
                # actually done by looping on pid
       attent /dev/dk1   # give what dkformat is waiting for
#      wait     # wait for format process to have finished
        wait $proc     # wait for format process to have finished
	
#      exitval='exit_value_of_last_process_started_in_background' ;;
	exitval="$?" ;;
  esac
case $exitval in
   0) echo "Floppy was formatted successfully" ;;
   *) echo "<beep>Format process failed. Try other floppy"
esac
}           # end of function
echo -n "Insert floppy and press return "
read return
doit & #start format process in background and exit immediately
------------------------------------------------------------------------
--
Casper H.S. Dik, casper at fwi.uva.nl,	NIC: !cd151



More information about the Comp.unix.shell mailing list