Invoking shell script from shell script

I am trying to invoke another shell script from a shell script and I am not sure of how to check the return code of the called script. Please help.

Thanks
Roshan

if your just looking for success just call the other script in an if statemend:

if $HOME/scripts/imi_reserve
then :
else DoErrorMessage "Call to imi_reserve failed in daily_deployment_ext, retcd = $?"
DoAbort
fi

I am not just checking for if the script ran or not. If I need to check the return code from the invoked program. Is this how I would do it ?

Have an exit code code specified for each action in the called program and then when I call it and check $? for the error code ?

Please correct me if I am wrong.

Thanks
Roshan

That looks about right to me...
To paraphrase:

(...)
/path/to/other/script
case $? in
0) Do something ;;
*) Do something else ;;
esac
(...)

or:

(...)
/path/to/other/script
if [ "$?" -ne "0" ]; then
freak out here
fi
(...)

There are several different ways of doing what you need.

Hope that helps.

Hi,

I tried code like this

echo " Enter user name :\c"
read resp

if (test $? -ne 0)
then
exit
fi

The above condition is not working if there is
a terminal hungup happens.
when I run the shell script it echoes
Enter user name :

At that time, when the user is not able to enter due to some
reason and the terminal is hungup. Still the process is running
without exiting.

How can we catch this kind of error ? Do we need to use
any signals ?

Thanks,
kk

A terminal hangup signal is a SIGHUP. See the trap man page for info on catching and handling signals in Bourne and Korn shell. See onintr for C shell.

What that's doing (I think), is evaluating if the "test " command was successful, which it will be any time your read statement succeeds.

And actually, I don't think you need the test command in there at all...
if [ "$?" -ne "0" ]; then

Or also, assuming you're using a fairly modern sh, you could even save a line of code and try:

read resp || exit 1

It'll simply exit (with status -ne 0) if the read statement fails. (BTW, those are two pipe symbols, no space inbetween)

Hi,

I tried the statement :
read resp || exit 1

But it didn't catch the hungup signal.
Any more ideas please ?

Thanks,
kk.

Sorry, I didn't fully understand your previous post.

You'd want to put something at the top of the script like this:

trap 'exit 1' 1

I don't know why it isn't doing that now, but this may help...
FYI, the syntax for a trap is:
trap '(what to do)' [signals to trap]
So the example above will tell the script inplicitly to 'exit 1' when it sees the HUP signal go by...

For a list of signals, issue the command:
kill -l
(lowercase L)

There are two separate questions in this posting but I think Roshan is asking this, if I am not mistaken.

step1.sh file looks like this:
------------------
next=step2.sh
this=step1.sh
logfile=mylog.log
.
.
#invoke next script:
. $HOME/bin/$next

# terminate processing if control comes back to this script
# or if $next is not found to execute
es=$?
if [[ $es != 0 ]]; then
print "`date +%T` Error could not start $next or it terminated"\
"abnormally; exit status was $es" >>$logfile
exit 1
else
exit 0
fi

Gianni

invoke the shell script using . XXXX.ksh(to execute it in the current shell). Give space after the "."
Then you can use
status=$?
if [ $status -eq 0 ]
then
echo "Invoked script is successful"
else
echo "Invoked shell script failed with the status of $status"
fi

have you tried it and is it not working