How to return a value from a shell script on Linux to a calling program on Hp Tru64 UNIX (228 Views)
Reply
Occasional Contributor
AKB_1
Posts: 4
Registered: ‎09-15-2008
Message 1 of 3 (228 Views)

How to return a value from a shell script on Linux to a calling program on Hp Tru64 UNIX

Hi All,

I am trying to return a value to a calling program on HP Tru64 UNIX from a shell script on Linux. In the program on linux, I use FTP to get a file and handle errors like unable to login etc. These errors are handled by doing a grep on the log file for "Unable to login" text and assigning a value to a variable called Login a value of 1 if the process could not login to the FTP Site. When this error occurs, I do a exit 1 but befor doing this, I want to return a value to the calling program to indicate that this process has failed and would like to use this returned value in the calling program to echo a message saying that the program has failed. How can I achieve this? Please advise.

Thanks
Aishwarya.
Honored Contributor
Steven Schweda
Posts: 9,091
Registered: ‎02-23-2005
Message 2 of 3 (228 Views)

Re: How to return a value from a shell script on Linux to a calling program on Hp Tru64 UNIX

What's "a calling program on HP Tru64 UNIX"?

Are you doing something like this on the
Tru64 system?:
rsh linux_host shell_script [args]

If so, then on the Tru64 system, the exit
status from the rsh command won't tell you
what the exit status was from the remote
command, so, as you've seen, "exit 1" there
does nothing for you.

The remote script _can_ write text to stdout
and/or stderr, and this can be examined by
the person or process which does the rsh.
For example, instead of
exit $sts
you could write it back on stderr:

dy # cat test.sh
#!/bin/sh
echo "$1"
sts=$(( "$1" + "$1" ))
echo "sts=$sts" >&2

(I have an HP-UX system up at the moment, so
pretend that "remsh" is "rsh".)

dy # remsh dy './test.sh 3'
sts=6
3

Here, the "3" was on stdout, and "sts=6" was
on stderr. If we redirect stdout:

dy # remsh dy './test.sh 3' > /dev/null
sts=6

then only the status string is seen.

The best way to do this may depend on what
the remote script is doing with stdout and
stderr now. If nothing, then you can do
practically anything you wish. One advantage
to using stderr for the status string is that
complaints from real problems will also go to
stderr:

dy # remsh dy './test.sh fred' > /dev/null
./test.sh[3]: fred + fred : The specified number is not valid for this command.
Honored Contributor
Rob Leadbeater
Posts: 3,582
Registered: ‎08-14-2002
Message 3 of 3 (228 Views)

Re: How to return a value from a shell script on Linux to a calling program on Hp Tru64 UNIX

Hi Aishwarya,

I'm not entirely sure how you're running things, however searching a log file for errors is likely to give you problems.

If I were you I would rewrite the script in Perl, using the Net::FTP module. This allows for much better error handling.


Cheers,

Rob
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.