Test exit status of last cmd via ssh

see below for a housekeeping script which constructs an ssh cmd using some server/path/sudo info found in $HRINST.
the script should hop to each server and if it finds a file to cleanup, moves it to the archive dir
if there is nothing to move, it should report so and email the output
currently i have got it working to perform the cleanup but struggling at the stage where it should report the error if nothing found.

was trying to perform a test on the exit status of the mv comd and if !=0 then report the error? not quite working as the cmd is issued via ssh. pls help


cat $HRINST | while read configline

do
        myhost=`echo $configline | awk -F":" '{print $1}'`
        mypath=`echo $configline | awk -F":" '{print $2}'`
        myuserid=`echo $configline | awk -F":" '{print $3}'`
        myinstance=`echo $mypath | awk -F"/" '{print $5}'`
        myyesterdaylog=`echo $myinstance| sed -e 's/_/\./g'`

        echo "Files cleaned up on  $myinstance"
        echo "                                                 "
        ssh -n $myuserid@$myhost "ls $mypath/OrderDump*"
        ssh -n $myuserid@$myhost "mv $mypath/OrderDump* $mypath/archive/"
        echo "                                                 "
        echo "                                                 "

done >>  $LOG 2>&1

echo "Executed housekeeping script cleanup.sh"  >>  $LOG 2>&1



function TestCleanup {

        if [ $? != 0 ]; then
              printf "nothing to remove in $myinstance"
        fi;
}

I don't get why you need ssh -n (-n option). But it won't affect what we are doing.
One easy way to get a status is to echo one. We use OK NOTOK

st=$(ssh  me@myhost  'some command >/dev/null 2>&1 &&  echo OK || echo NOTOK' )
[ "$st" = "OK" ] || echo "failed on $myhost" | mailx -s "$myhost error" me@home
1 Like

cool that works. although first i thought you made an error with "st" = "OK" so i tried NOTOK first. i.e if the result of the previous cmd was not ok then report failure. but that didnt work, your line worked. why is that? how am i reading that?

this is how ive implemented it

  st=$(ssh $myuserid@$myhost 'mv $mypath/OrderDump* $mypath/archive > /dev/null 2>&1 && echo OK || echo NOTOK')
        [ "$st" = "OK" ] || echo "failed on $myhost"

---------- Post updated at 09:51 AM ---------- Previous update was at 09:43 AM ----------

wait a sec, doesnt quite work well. it still echos the failure text even if the cmd is sucessfull

so in the below both myssh1 and myssh2 were OK (meaning to say they did what they were meant to i.e ls to show the files and mv to archive them)

   myssh1=$(ssh $myuserid@$myhost 'ls $mypath/OrderDump*  > /dev/null 2>&1 && echo OK || echo NOTOK')
        [ "$myssh1" = "OK" ] || echo "No Order Dump files found on $myhost"

        myssh2=$(ssh $myuserid@$myhost 'mv $mypath/OrderDump* $mypath/archive > /dev/null 2>&1 && echo OK || echo NOTOK')
        [ "$myssh2" = "OK" ] || echo "Nothing to clean up on $myhost"

yet the result was

  1. files were archived off
  2. output text below

No Order Dump files found on <host>
Nothing to clean up on <host>

---------- Post updated at 10:16 AM ---------- Previous update was at 09:51 AM ----------

and does the || apply if the ls returns

ls: /OrderDump*: No such file or directory

I believe so, ls should return an error code as expected there.