Failed to check status code in "rsh" command

Hi folks,

I wrote a ksh program which run scripts from remote server.
To check the status code I wrote the following function:

check_remote_status()
{
status_code=`tail -1 $installLog`
if [[ ${status_code} -ne 0 ]] ; then
  echo $errMsg | tee -a $installLog
  exit 1
else
  echo $validMsg >> $installLog
fi
}

The main function is:

run_install_sr()
{
cat ${CONF_FILE} | grep INSTALL_SR_ | grep :Y | while read line
do
  INSTALL_SR_DIR=`echo $line | cut -d= -f2 | cut -d: -f1`
  errMsg="ERROR: Failed to copy ${RESPONSE_FILE_SR} to ${DB_HOST_NAME}!"
  validMsg="-> VALID: ${RESPONSE_FILE_SR} was copied to ${DB_HOST_NAME}."
  rcp ${RESPONSE_FILE_SR} ${DB_HOST_NAME}:/tmp >> $installLog
  check_remote_status

  errMsg="ERROR: Failed to run SR database layer!"
  validMsg="-> VALID: SR Database layer completed successfully."
  rsh ${DB_HOST_NAME} "su - root -c \"cd ${INSTALL_SR_DIR} && ./Install_SR.ksh -f /tmp/${RESONSE_FILE_SR##*/} -l database"'; echo $?"' | tee -a $installLog
  check_remote_status

  errMsg="ERROR: Failed to copy ${RESPONSE_FILE_SR} to ${ADMIN_HOST_NAME}!"
  validMsg="-> VALID: ${RESPONSE_FILE_SR} was copied to ${ADMIN_HOST_NAME}."
  rcp ${RESPONSE_FILE_SR} ${ADMIN_HOST_NAME}:/tmp >> $installLog
  check_remote_status

  errMsg="ERROR: Failed to run SR RiGHTv administration layer!"
  validMsg="-> VALID: SR RiGHTv administration layer completed successfully."
  rsh ${ADMIN_HOST_NAME} "su - root -c \"cd ${INSTALL_SR_DIR} && ./Install_SR.ksh -f /tmp/${RESPONSE_FILE_SR##*/} -l administration"'; echo $?"' | tee -a $installLog
  check_remote_status

  if [[ ${RTESUB_LAYER} = 'YES' ]] && [[ ${ADMIN_LAYER} = 'YES' ]] ; then

     errMsg="ERROR: Failed to copy ${RESPONSE_FILE_SR} to ${RTESUB_HOST_NAME}!"
     validMsg="-> VALID: ${RESPONSE_FILE_SR} was copied to ${RTESUB_HOST_NAME}."
     rcp ${RESPONSE_FILE_SR} ${RTESUB_HOST_NAME}:/tmp >> $installLog
     check_remote_status

     errMsg="ERROR: Failed to run SR RTE subscriber layer!"
     validMsg="-> VALID: SR RTE subscriber layer completed successfully."
     rsh ${RTESUB_HOST_NAME} "su - root -c \"cd ${INSTALL_SR_DIR} && ./Install_SR.ksh -f /tmp/${RESPONSE_FILE_SR##*/} -l subscriber"'; echo $?"' | tee -a $installLog
     check_remote_status
  fi
done
}

When the program call to check_remote_status function ,I get the following error message to the screen:
E.g:
./RiGHTvInstaller.ksh[2]: -> VALID: SR RiGHTv administration layer completed successfully.: syntax error

Any ideas why?

Thanks in advance,
Nir

Normally there are two status "things" to check for remsh and rsh.
Your method will work if the remote script were to echo $? a different way, like this:
My example is remsh, but it doesn't matter

rc=$(remsh somenode "A really long comand && echo 'OKAY'")
status=$?
if [[ $status -eq 0 ]]    # remsh command successful, now get remsh rc...
        then
            if [[ `echo "$rc" | grep -q '^OKAY$'` -eq 0 ]]
            then
                echo " all is well"
            else
                echo "remote failure"
            fi
else
     echo "local failure: remsh"
fi

Hi Jim,

Thanks!
I'll test your suggestion.

Best regards,
Nir

Hi Jim,

I wrote a small program:

#! /bin/ksh

CONF_FILE=nir.conf
export $(< $CONF_FILE)
run_install_sr()
{
nawk -F'[=:]' '$1 ~ "^INSTALL_SR_" && $NF = "Y" {print $2}' "${CONF_FILE}" | while read INSTALL_SR_DIR
do
  echo Install SR directory is $INSTALL_SR_DIR
  rc=$(rsh ${DB_HOST_NAME} hostname && echo OKAY)
status=$?
if [[ $status -eq 0 ]]    # remsh command successful, now get remsh rc...
        then
            if [[ echo "$rc" | grep -q '^OKAY$' ]]
            then
                echo " all is well"
            else
                echo "remote failure"
            fi
else
     echo "local failure: remsh"
fi

done
}
run_install_sr

I ran it :

buffy> ./nir.ksh
./nir.ksh[5]: syntax error at line 14 : `"$rc"' unexpected

And as you can see ,I failed.

I'll be glad to get your help again.

Thanks in advance,
Nir

nir_s, I think that you have the syntax wrong there. Replace

if [[ echo "$rc" | grep -q '^OKAY$' ]]

with

if [[ "$rc" == 'OKAY' ]]

Thanks pal but now I'm getting another syntax error:

./nir.ksh[5]: rc=${rsh ${DB_HOST_NAME} hostname && echo OKAY}: bad substitution

Shouldn't it be

rc=$(rsh ${DB_HOST_NAME} hostname && echo OKAY)

Hey vino my freiend!

Thanks!
You solved my syntax problem,but I still have a funcionality problem with the condition.

Following nir.ksh content:

#! /bin/ksh

CONF_FILE=nir.conf
export $(< $CONF_FILE)
run_install_sr()
{
nawk -F'[=:]' '$1 ~ "^INSTALL_SR_" && $NF = "Y" {print $2}' "${CONF_FILE}" | whi
le read INSTALL_SR_DIR
do
  rc=$(rsh ${DB_HOST_NAME} hostname && echo OKAY)
status=$?
if [[ $status -eq 0 ]]    # remsh command successful, now get remsh rc...
        then
            if [[ "$rc" = 'OKAY' ]]
            then
                echo " all is well"
            else
                echo "remote failure"
            fi
else
     echo "local failure: remsh"
fi

done
}
run_install_sr

The problem is with the condition:
if [[ "$rc" = 'OKAY' ]]
then
echo " all is well"
else
echo "remote failure"
fi

When I run the script I get the output : "remote failure",despite of the fact that there is no problem to get to the remote server.

I want to check the status which returned back from a remote server.
How to do it right?

Thanks in advance,
Nir

  rc=$(rsh ${DB_HOST_NAME} hostname && echo OKAY)
status=$?

I think it must have something to do with the above code.

Try something like

rsh ${DB_HOST_NAME} hostname
status=$?
if [[ $status -eq 0 ]] ; then
  rc=OKAY
fi ;

Thanks pal but I'm still does not catch the correct status from the command I run on the remote server.

My code is now as the following:

CONF_FILE=nir.conf
export $(< $CONF_FILE)
run_install_sr()
{
nawk -F'[=:]' '$1 ~ "^INSTALL_SR_" && $NF = "Y" {print $2}' "${CONF_FILE}" | whi
le read INSTALL_SR_DIR
do
  rsh ${DB_HOST_NAME} hostnam
  status=$?
  if [[ $status -eq 0 ]] ; then
    rc=OKAY
  fi
            if [[ "$rc" = 'OKAY' ]]
            then
                echo " all is well"
            else
                echo "remote failure"
            fi
done
}
run_install_sr

I changed the command "hostname" to "hostnam"
I ran the script:

bounty:# ./nir.ksh 
ksh: hostnam: not found
 all is well

I expected to get "remote failure" and not "all is well".

Any more ideas?

Thanks in advance,
Nir