How to capture a return code of a command in shell script

Hi,

I have written a shell script to execute a command as

....
....
"$@" > /dev/null &
....
....
....

Now, this command will run in the background. It will run for a while before it will terminate.
I have written a code to continue checking the progress and will terminate the process if I need to.

Now the question is... if the command execute successfully, how do I check the return code from the command? What I need to do to get this information.

Thank you.

xewooxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

patrick_defibaugh_chavezCommented:
You should use the popen system calls:

http://www.opengroup.org/onlinepubs/007908799/xsh/popen.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
xewooxAuthor Commented:
I want to do this in a shell script and not in C/C++ program.

0
patrick_defibaugh_chavezCommented:
You need to capture it when you execute the script.  I would write another shell script called print_exit_status.sh to call your first shell script as so:

print_exit_status.sh:
if my_script ; then
         echo my_script exited correctly
else
         echo my_script exited with an error
fi

Then call the new scirpt like so:
sh print_exit_status.sh > my_script_return_value.txt &

This should redirect the echo statments of the new script to a text file.
0
xewooxAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.