Xref: utzoo comp.unix.questions:26861 comp.unix.shell:883 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!mips!prls!pyramid!infmx!dberg From: dberg@informix.com (David I. Berg) Newsgroups: comp.unix.questions,comp.unix.shell Subject: Re: exit value Message-ID: <1990Nov13.233445.22563@informix.com> Date: 13 Nov 90 23:34:45 GMT References: <1990Nov12.110248@cs.utwente.nl> <1990Nov13.160213.15743@ssd.kodak.com> Sender: news@informix.com (Usenet News) Organization: Informix Software, Inc. Lines: 39 >In article <1990Nov12.110248@cs.utwente.nl> stadt@cs.utwente.nl (Richard van de Stadt) writes: >> >> >>How do I get the return value of a shell script or program that was >>started (from a shell script) in the background? > >#!/bin/csh -fb #should also work with /bin/sh > >rm not_a_real_file_to_get_error >& /dev/null # redirection necessary for > # background processes >#this must be the very next statement >echo "status of rm was $status" > /dev/tty # could be redirected to > # any file There are two possibilities here; one, you merely want to retrieve the exit status of a spawned processes, or two, you want to return a value generated by the spawned process, or both) The above illustrated example does the former, assuming the spawned process exits with a status (eg. exit n, where n is the status value). The following example does both: In the foreground script: # set answer = `create_answer` if ($status > 0) then echo "Message about create_answer if status > 0" exit end if echo $answer In the script create answer: # set xyz = some value or algorithm set exitcode = some value to indicate success or failure (or anything else) of this script. (0 usually means normal execution; > 0 otherwise) echo $xyz exit $exitcode