Operating System - HP-UX
1835079 Members
2357 Online
110073 Solutions
New Discussion

Re: passing result codes between servers and scripts

 
John McWilliams
Advisor

passing result codes between servers and scripts

I am running a script on a HP-UX 10.20 server that remotely runs a script on another server. How can I pass the result code of the remote script commands to the original server.

I also run 2 scripts on the same server and want to pass the result code fromthe second script to the first. How can I do this.

Thanks John
5 REPLIES 5
Peter Kloetgen
Esteemed Contributor

Re: passing result codes between servers and scripts

Hi John,

for the first point i actually don't know a solution. Your second problem:

--> I think the second script is called in the first script, is it?
---> Then you could call it and in the script you leave the second script with an exit- command which enables you to get the desired exitcode. Enter the following lines into your script:

your_script_command_lines
exit 1

In your first script put the following line immediately after exiting script 2:

var=$?

This remembers the exit code you gave to second script.

echo $var would give you the exit value... 1

You can give exit values from 0-255.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Justo Exposito
Esteemed Contributor

Re: passing result codes between servers and scripts

Hi John,

Between scripts you can use the $? variable in order to know the return value, and in the second script you must put the exit command with a value like this:
exit 2
exit $Variable

Between two boxes with remsh you can call the second script as:
Script1 in host1
var=$(remsh host2 -n scriptname)

Script2 in host2
echo "2"
exit

Hope this helps,

Justo.

Help is a Beatiful word
Nick Wickens
Respected Contributor

Re: passing result codes between servers and scripts

On the same machine you can put the code into a variable and then export the variable. ie

RESULT=value
export RESULT

I have not tried exporting to other servers but if it doesn't work why not put the result in a temp file available to both servers ?
Hats ? We don't need no stinkin' hats !!
Frank Slootweg
Honored Contributor

Re: passing result codes between servers and scripts

I think your second question (2 scripts on same server) has been answered, so I will only address your first question (2 scripts on 2 servers).

Write the exit code with an echo command and use command substitution (`....`) to put it in a local variable.

A small example:

$ remsh remote_sys "ll found"
-rw-r--r-- 1 franks ftp_group 0 Jul 3 17:04 found
$ remsh remote_sys "ll not_found"
not_found not found
$ remsh remote_sys "cat doit"
ll $1 >/dev/null 2>&1
echo $?
$ result=`remsh remote_sys "doit found"`
$ echo $result
0
$ result=`remsh remote_sys "doit not_found"`
$ echo $result
2
$
A. Clay Stephenson
Acclaimed Contributor

Re: passing result codes between servers and scripts

Hi John:

I'll address your first question since the other has been covered.

Your question has come up many times. I will simply duplicate one of my earlier posts.
NOTE: This is the most general solution to the problem when you also need STDOUT and STDERR from your remsh'ed process. i.e. This method works in all cases.

The remsh exit code of 0 only means that the script was able to be launched. The exit status of the remote command is not returned. There is a workaround I have used for a long time.

REM_ERR_FILE=/var/tmp/x${$}.err
remsh remote_host my_command ${REM_ERR_FILE}
LOCAL_STAT=$?

${LOCAL_STAT} only tells us about the success of remsh itself

Now my_command on the remote host can get the
value of ${REM_ERR_FILE}; any status values you are interested in can be written to that file by your script on the remote host.

When the remsh command finishes you can then
REM_STAT=$(remsh remote_host cat ${REM_ERR_FILE})
to capture the remote commands status. (You should then issue a final remsh to remove the ${REM_ERR_FILE}.)

It's a little complicated but it does work. Since we generate a process id dependent filename on the local host, you don't have to worry about filename collision when multiple instances are running. This method also leaves stderr and stdout for their normal use.

You can serach the Forums for remsh and status for other methods.
Regards, Clay
If it ain't broke, I can fix that.