Send correct exit code from child script back to parent

Hello all; hope someone can help me cause I am going crazy trying to find a solution for (what I think is simple) issue...looked hard up and down this forum and tried several "solutions" with no avail...so here's my issue:

I have this (parent) script: copylsofdcmcadefttosftpwithmove.sh

Inside this script I have this piece of code:

if [ "$flag" = "yes" ]
then
        /usr/local/bin/ssh $sftpserver mv $sftppath/$filenameshort $finaldirname  >> ${_ftpdebug} 2>&1
        ssh faunus:/home/dslmain/scripts/senddcmcadefttest.sh
        if [ $? -ne 0 ]
        then
           echo "EXIT: " $?
           exit
        fi
        echo "DONE"
else
        echo "FAILED"
fi

I call another script via ssh (red text).

This is the contents of the script: senddcmcadefttest.sh

#!/bin/sh -x
node=fid
NODE=FAUNUS
_Ymd=`date +%Y%m%d`
indir=/home/dslmain/export/${node}/eft/in
sentdir=/home/dslmain/export/${node}/eft/sent
LOG="/home/dslmain/export/$node/eft/logs/eft.log.`date +%m%d`"
_temp=/utemp/tmp_logs/eftexists
if [ -s $_temp ]
then
  rm $_temp
fi
_ym=`date +%m%d`

ls ${indir}/*${_ym}* > $_temp
if [ ! -s $_temp ]
then
   echo "No EFT files detected" >> ${LOG}
   exit 1
fi
cd ${indir}
for FILE in `cat $_temp|awk -F'/' '{print $NF}'`
do
  if [ -s ${FILE} ]
  then
    echo "Here _UPLOAD function would have run"
    echo "Snapshot of files of the day found on RBC FTP" >> ${LOG}
    A=1
    B=1
    echo $A >> ${LOG}
    echo $B >> ${LOG} 
    if [ ${A} -eq ${B} ]
    then
      echo "${FILE} was successfully uploaded to RBC" >> ${LOG}
      echo ""  >> ${LOG}
      mv ${indir}/${FILE} ${sentdir}/${FILE}
      exit 0
    else
      echo "${indir}/${FILE} failed to be loaded to RBC" >> ${LOG}
      echo " "  >> ${LOG}
      exit 2
    fi
  else
    echo "Sorry file dose not exist, or file is empty" >> ${LOG}
    exit 3
  fi
done

So in the child script I have 3 different scenarios (in red) where the exit code will be either 0, 2 or 3...I want to pass the correct exit code back to the parent script copylsofdcmcadefttosftpwithmove.sh so that it can be evaluated in the "if" statement...however $? is always coming back as "0"...

Thanks for reading and any assistance.

Regards
Giuliano

You are getting the return code from ssh, not your script.

In the main script can you try this :

instead

ssh faunus:/home/dslmain/scripts/senddcmcadefttest.sh
this :

ssh user@remote_machine "/home/dslmain/scripts/senddcmcadefttest.sh ; exit \$?"

Basically this way I make sure that the ssh ends with same exit status as the remotely launched script . Hope the ssh method suites you, is the way I used it and worked for me.

---------- Post updated at 09:04 AM ---------- Previous update was at 08:54 AM ----------

If this doesn't work I'd also suggest a basic check: launching the remote script directly, on the remote machine and make sure that it returns the desired exit status codes. To make sure that the exit status is ok but you;re just not able to capture it and eliminate the possibility that the exit status is wrong ( "0" ) and you';re capturing it right.

1 Like

Thanks black_fender worked splendidly.