exit status of rsh

exit status of rsh

Post by Michael Ziege » Fri, 21 Sep 2001 21:52:01



Hi there!

Is there any way to check if an rsh failed? I mean the man page says:
------
EXIT STATUS
     The following exit values are returned:

     0         Successful completion.

     1         An error occurred.
------
But:
------
% csh
% exit 1
% % echo $status
1
% sh
$ exit 1
% echo $status
1
% rsh `hostname` exit 1
% echo $status
0
%
------
Huh??? Any hints are welcome.

Thanks,
Michael
--
************************************************************************
Michael Zieger
Consulting Engineer           Methodology Services Europe
CADENCE Design Systems GmbH   Phone: +49-89-4563-1851
Mozart Str. 2                 Fax:   +49-89-4563-1819

************************************************************************

 
 
 

exit status of rsh

Post by Hanspeter 'Happl' Oberli » Fri, 21 Sep 2001 21:59:02




Quote:>Is there any way to check if an rsh failed?

                              ^^^^^^
The rsh itself or the command executed on the remote system?          

Quote:>% rsh `hostname` exit 1
>% echo $status
>0

That's true, because 0 is the exit status of rsh.

It's possible, but you have to use a workaround (example
in Bourne Shell, not tested; $? should be $status):

CSTAT=`rsh \`hostname\` "command >/dev/null 2>&1; echo \$?"`
echo "status rsh = $?"
echo "status command = $CSTAT"

Greetings from Switzerland -- Gruesse aus der Schweiz
Happl

 
 
 

exit status of rsh

Post by Michael Ziege » Fri, 21 Sep 2001 22:58:26





> >Is there any way to check if an rsh failed?
>                               ^^^^^^
> The rsh itself or the command executed on the remote system?

Actually the command executed on the remote system.

Quote:

> >% rsh `hostname` exit 1
> >% echo $status
> >0

> That's true, because 0 is the exit status of rsh.

Yes, but if I tell it to terminate with exit status 1, why does it
return 0 anyway? I guess it's because rsh doesn't just create a new
shell, but it triggers inetd to start an in.rlogind which then creates
a new shell, so the exit code of the actual shell gets lost somewhere
in the hierarchy.

Quote:

> It's possible, but you have to use a workaround (example
> in Bourne Shell, not tested; $? should be $status):

> CSTAT=`rsh \`hostname\` "command >/dev/null 2>&1; echo \$?"`
> echo "status rsh = $?"
> echo "status command = $CSTAT"

Good idea! Since I'm using csh I had to modify it a little bit:
% set name = `hostname`
% set cstat = `rsh $name 'bla >&/dev/null;echo $status'`
% echo $cstat
1

Obviously csh doesn't understand nested backticks. However, this doesn't
solve my real problem. I need a shell script to execute some commands with
a different uid (provided that I'm listed in the corresponding .rhosts
file). That problem is solved:

/bin/rsh -l $remote_user `hostname` /bin/csh -f -s <<EOF
some
commands
here
EOF

Now I want to know if any of these commands exited with a non-zero
status and if yes, I want to terminate the shell script with the same
exit status. This is important for error handling in the process that
is the parent of the script. Any ideas?

Thanks,
Michael

Quote:

> Greetings from Switzerland -- Gruesse aus der Schweiz
> Happl

--
************************************************************************
Michael Zieger
Consulting Engineer           Methodology Services Europe
CADENCE Design Systems GmbH   Phone: +49-89-4563-1851
Mozart Str. 2                 Fax:   +49-89-4563-1819

************************************************************************
 
 
 

exit status of rsh

Post by Hanspeter 'Happl' Oberli » Fri, 21 Sep 2001 23:08:12




Quote:>> >% rsh `hostname` exit 1
>> >% echo $status
>> >0
>> That's true, because 0 is the exit status of rsh.
>Yes, but if I tell it to terminate with exit status 1, why does it
>return 0 anyway? ...

Because $status is the status of the last command on the
local mashine and that's rsh.

Quote:>Now I want to know if any of these commands exited with a non-zero
>status and if yes, I want to terminate the shell script with the same
>exit status. This is important for error handling in the process that
>is the parent of the script. Any ideas?

This is geting complicated... :-)

... <<EOF
STAT=0
command1
if test $? -gt $STAT; then STAT=$?; fi
command2
if test $? -gt $STAT; then STAT=$?; fi
echo $STAT
EOF

By this, you'll get the highest exit status.

Greetings from Switzerland -- Gruesse aus der Schweiz
Happl

 
 
 

exit status of rsh

Post by Michael Ziege » Fri, 21 Sep 2001 23:55:22





> >> >% rsh `hostname` exit 1
> >> >% echo $status
> >> >0
> >> That's true, because 0 is the exit status of rsh.
> >Yes, but if I tell it to terminate with exit status 1, why does it
> >return 0 anyway? ...

> Because $status is the status of the last command on the
> local mashine and that's rsh.

> >Now I want to know if any of these commands exited with a non-zero
> >status and if yes, I want to terminate the shell script with the same
> >exit status. This is important for error handling in the process that
> >is the parent of the script. Any ideas?
> This is geting complicated... :-)

> ... <<EOF
> STAT=0
> command1
> if test $? -gt $STAT; then STAT=$?; fi
> command2
> if test $? -gt $STAT; then STAT=$?; fi
> echo $STAT
> EOF

> By this, you'll get the highest exit status.

> Greetings from Switzerland -- Gruesse aus der Schweiz
> Happl

I think I got it:
------
# don't play tricks on me
set grep   = /bin/grep
set awk    = /bin/awk
set csh    = /bin/csh
set rsh    = /bin/rsh
set rm     = /bin/rm
set cat    = /bin/cat
unalias *

set hostname = `hostname`
# prepare error handling
if(-e /tmp/error.$$) then
  $rm -f /tmp/error.$$
endif
$rsh -l $admin_name $hostname $csh -f -s <<EOF
...
# create the project's directory structure
source $mkproject_file
set savestate = \$status
if("\$savestate" != "0") then
  echo \$savestate > /tmp/error.$$
  exit \$savestate
endif
...
EOF
# Did any of the commands fail?
if(-e /tmp/error.$$) then
  set exitstate = `$cat /tmp/error.$$`
  $rm -f /tmp/error.$$
  exit $exitstate
endif
------

I want to exit on the first error, so I don't need to check for the
highest exit status.

Thanks,
Michael
--
************************************************************************
Michael Zieger
Consulting Engineer           Methodology Services Europe
CADENCE Design Systems GmbH   Phone: +49-89-4563-1851
Mozart Str. 2                 Fax:   +49-89-4563-1819

************************************************************************

 
 
 

exit status of rsh

Post by Darren Dunha » Sat, 22 Sep 2001 02:11:05



Quote:> Hi there!
> Is there any way to check if an rsh failed? I mean the man page says:
> ------
> EXIT STATUS
>      The following exit values are returned:
>      0         Successful completion.
>      1         An error occurred.
> ------
> But:
> ------
> % csh
> % exit 1
> % % echo $status
> 1
> % sh
> $ exit 1
> % echo $status
> 1
> % rsh `hostname` exit 1
> % echo $status
> 0
> %
> ------
> Huh??? Any hints are welcome.

Can you switch to ssh?  It passes the exit code of the remote shell to
your local shell.  That seems to be what you want.

--

Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
          < How are you gentlemen!! Take off every '.SIG'!! >

 
 
 

exit status of rsh

Post by Michael Ziege » Sun, 23 Sep 2001 02:13:46




> > Hi there!

> > Is there any way to check if an rsh failed? I mean the man page says:
> > ------
> > EXIT STATUS
> >      The following exit values are returned:

> >      0         Successful completion.

> >      1         An error occurred.
> > ------
> > But:
> > ------
> > % csh
> > % exit 1
> > % % echo $status
> > 1
> > % sh
> > $ exit 1
> > % echo $status
> > 1
> > % rsh `hostname` exit 1
> > % echo $status
> > 0
> > %
> > ------
> > Huh??? Any hints are welcome.

> Can you switch to ssh?  It passes the exit code of the remote shell to
> your local shell.  That seems to be what you want.

Sounds good, but I have to use what comes with the OS. Can't count on
ssh being installed everywhere. However, I found another solution:

# don't play tricks on me
set csh    = /bin/csh
set rsh    = /bin/rsh
set rm     = /bin/rm
set touch  = /bin/touch
unalias *
set hostname = `hostname`
i# prepare error handling
f(-e /tmp/error.$$) then
  $rm -f /tmp/error.$$
endif
$rsh -l $admin_name $hostname $csh -e -f -s <<EOF
# create an 'error-flag'
$touch /tmp/error.$$
some
commands
# remove the 'error-flag' if we reached this line
$rm -f /tmp/error.$$
EOF
# check for the 'error flag'
if(-e /tmp/error.$$) then
  $rm -f /tmp/error.$$
  exit 1
endif

Thanks,
Michael

> --

> Unix System Administrator                    Taos - The SysAdmin Company
> Got some Dr Pepper?                           San Francisco, CA bay area
>           < How are you gentlemen!! Take off every '.SIG'!! >

--
************************************************************************
Michael Zieger
Consulting Engineer           Methodology Services Europe
CADENCE Design Systems GmbH   Phone: +49-89-4563-1851
Mozart Str. 2                 Fax:   +49-89-4563-1819

************************************************************************
 
 
 

exit status of rsh

Post by Heiner Steve » Sun, 23 Sep 2001 06:31:59


 > Is there any way to check if an rsh failed? I mean the man page says:
 > ------
 > EXIT STATUS
 >      The following exit values are returned:
 >      0         Successful completion.
 >      1         An error occurred.
[...]

It is a common misconception that "rsh" returns the exit status
of the command executed.

"rsh" establishes a network connection, handles authentication
(.rhosts), sets up standard input and standard output for
remote command execution, and finally arranges for the command
to be remotely executed.

The "rsh" command only reports its own error code: if the
remote connection could be established successfully, and the further
actions finish successfuly, it returns success.

This is independant of the command that is run remotely.

Heiner
--
 ___ _

\__ \  _/ -_) V / -_) ' \    Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_|   http://www.shelldorado.com/

 
 
 

1. exit status using rsh

Hi

Is it possible to get exit status of a program started with rsh, so I can use
it in a shell script (Bourne).
The problem I have is when pipe my stuff from one command on one machine
and want a while loop to terminate when the remote end fails, my rsh
returns that everything was okay.... forever.... (and as it is the last command
it takes precendence over the first one):

while  (  local_command| rsh remote_host remote_command )
.....Do Something

A workaround could be to swap the remote and local entry, which will
work in some sitaution (when it does not matter which machine is used for
local and remote operation),

while  (  rsh remote_host original_local_command| original_remote_command )
.....Do Something

however does anyone know a way to get original loop to terminate
corrrectly, I would be happy

-Morten

--

------------------------------------------------------------------------
Be cautious in your daily affairs.
------------------------------------------------------------------------

2. Virtual Disk Space

3. How to get the exit status of a remote command executed via rsh

4. aix-digest V3 #908

5. ersh - rsh frontend hack returning exit status of remote cmd

6. ISDN + IPPPD + problems.

7. how to force rsh to exit with status of remote command

8. KDE startup screen under redhat 6.2

9. rsh exit status

10. Getting exit status for a command completed by rsh.

11. rsh exit status using ersh

12. Exit status in trapped exit function

13. rsh - getting the status of an rsh'd command