第220行:返回:只能从函数或源脚本“返回”

第220行:返回:只能从函数或源脚本“返回”

# C A L L   C U S T O M E R   D E F I N E D   P A R A M E T E R S

#----------------------------------------------------------------------------------

cd /ERQ/100/DM/SCRIPTS

. ./DMPR2ERQ_log_interface.env



#----------------------------------------------------------------------------------

# I N T E R N A L   S E C T I O N   ! ! !

#----------------------------------------------------------------------------------



ORG_PWD=$PWD                                        # save directory where i am

COUNT=0                                             # actual value for retry logic

DATE=`date +'%y%m%d%H%M%S'`                         # creating time stamp

HUMAN_DATE=`date +'%d.%m.%Y - %H:%M:%S'`            # creating human readable time stamp

TMP_LOGFILE=$LOG_FILE_OUT.$$                        # temporary log file with PID

SLEEP_TIME=10                                       # how long to wait until check of file size



#----------------------------------------------------------------------------------

# S E N D  E M A I L  O N  E R R O R

#----------------------------------------------------------------------------------



SUBJECT="ERROR in script send_DMPR2ERQ_log.sh for Queue $QUEUE"



function error_mail

  {

  BODY="\nDate:   $HUMAN_DATE CET \

  \nScript: $ORG_PWD/send_DMPR2ERQ_log.sh \

  \nServer: `hostname` \

  \n\nThe following error occured: \

  \n---------------------------- \

  $ERROR_MSG"



  echo "$BODY" | mailx -s "$SUBJECT" "$EMAIL_ADDRESS"

  }



#----------------------------------------------------------------------------------

# R E T U R N C O D E S

#----------------------------------------------------------------------------------

RC_OK=0                                             # used in spoolin and spoolout

RC_PARAM_ERR=1                                      # used in spoolin and spoolout

RC_LOG_ERR=10                                       # used in spoolin and spoolout

RC_SI_PATH_ERR=11                                   # used in spoolin

RC_SO_PATH_ERR=12                                   # used in             spoolout

RC_ARCHIVE_PATH_ERR=13                              # used in spoolin

RC_ERROR_PATH_ERR=14                                # used in spoolin

RC_NO_FILES_ERR=15                                  # used in spoolin

RC_NO_QMGR_ERR=20                                   # used in spoolin and spoolout

RC_NO_QUEUE_ERR=21                                  # used in spoolin and spoolout

RC_NO_MQSERVER_ERR=22                               # used in spoolin and spoolout

RC_DPBAT_ERR=100                                    # used in spoolin and spoolout

RC_NO_FILENAME_ERR=300

RC_FILENAME_ERR=310



#----------------------------------------------------------------------------------

# E R R O R   S E C T I O N

#----------------------------------------------------------------------------------

SCRIPT_RC=$RC_OK                                    # init return code





if touch $LOG_FILE_OUT                              # log file access?

  then

    echo "\n["$DATE"]" > $TMP_LOGFILE

  else

    echo "ERROR: RC=$RC_LOG_ERR - Can't create $LOG_FILE_OUT"

    return $RC_LOG_ERR

fi



VARCHECK=`echo $MHQ_MQMGRNAME | wc -w`

if [ $VARCHECK = "0" ]                              # Queue Manager defined?

  then

    echo "ERROR: RC=$RC_NO_QMGR_ERR - Queue Manager not defined" >> $TMP_LOGFILE

    ERROR_MSG=$ERROR_MSG"\nERROR: RC=$RC_NO_QMGR_ERR - Queue Manager not defined!"

    SCRIPT_RC=$RC_NO_QMGR_ERR

fi



VARCHECK=`echo $QUEUE | wc -w`

if [ $VARCHECK = "0" ]                              # Queue defined?

  then

    echo "ERROR: RC=$RC_NO_QUEUE_ERR - Target Queue not defined" >> $TMP_LOGFILE

    ERROR_MSG=$ERROR_MSG"\nERROR: RC=$RC_NO_QUEUE_ERR - Target Queue not defined"

    SCRIPT_RC=$RC_NO_QUEUE_ERR

fi



VARCHECK=`echo $MQSERVER | wc -w`

if [ $VARCHECK = "0" ]                              # MQ Server connectivity defined?

  then

    echo "ERROR: RC=$RC_NO_MQSERVER_ERR - Variable MQSERVER not defined" >> $TMP_LOGFILE

    ERROR_MSG=$ERROR_MSG"\nERROR: RC=$RC_NO_MQSERVER_ERR - Variable MQSERVER not defined"

    SCRIPT_RC=$RC_NO_MQSERVER_ERR

fi



VARCHECK=`echo $ARCHIVE_PATH | wc -w`

if [ $VARCHECK = "0" ]                              # Archive path defined?

  then

    echo "ERROR: RC=$RC_ARCHIVE_PATH_ERR - Variable ARCHIVE_PATH not defined" >> $TMP_LOGFILE

    ERROR_MSG=$ERROR_MSG"\nERROR: RC=$RC_ARCHIVE_PATH_ERR - Variable ARCHIVE_PATH not defined"

    SCRIPT_RC=$RC_ARCHIVE_PATH_ERR

fi



VARCHECK=`echo $ERROR_PATH | wc -w`

if [ $VARCHECK = "0" ]                              # Error path defined?

  then

    echo "ERROR: RC=$RC_ERROR_PATH_ERR - Variable ERROR_PATH not defined" >> $TMP_LOGFILE

    ERROR_MSG=$ERROR_MSG"\nERROR: RC=$RC_ERROR_PATH_ERR - Variable ERROR_PATH not defined"

    SCRIPT_RC=$RC_ERROR_PATH_ERR

fi



VARCHECK=`echo $SI_PATH | wc -w`

if [ $VARCHECK != "0" ]                             # parameter SI_PATH defined?

  then

    cd $SI_PATH

    CD_RC=$?

    if [ $CD_RC = "0" ]

      then

                echo "--> Changed to spoolin directory $SI_PATH successfully." >> $TMP_LOGFILE

      else

                echo "ERROR: RC=$RC_SI_PATH_ERR - could not change to spoolin directory $SI_PATH" >> $TMP_LOGFILE

                ERROR_MSG=$ERROR_MSG"\nERROR: RC=$RC_SI_PATH_ERR - could not change to spoolin directory $SI_PATH"

                SCRIPT_RC=$RC_SI_PATH_ERR

    fi

  else

    echo "ERROR: RC=$RC_SI_PATH_ERR - no variable for spoolin directory specified" >> $TMP_LOGFILE

    ERROR_MSG=$ERROR_MSG"\nERROR: RC=$RC_SI_PATH_ERR - no variable for spoolin directory specified"

    SCRIPT_RC=$RC_SI_PATH_ERR

fi



#----------------------------------------------------------------------------------

# S P O O L I N

#----------------------------------------------------------------------------------

if [ $SCRIPT_RC -eq "0" ]

then

  #----------------------------------------------------------------------------------

  # F I L E   P O L L I N G

  #----------------------------------------------------------------------------------

    cd /srv/ERQ_P2M_DUALACCESS/DM/LOG

    cp `ls -tr DMPR2ERP* | tail -1` /ERQ/100/DM/OUTBOUND



cd /ERQ/100/DM/OUTBOUND   

for FILE in `ls -l DMPR2ERP*`; do

  if [ -f $FILE ]; then

    FILE_SIZE_1=`ls -l $FILE | awk '{ printf("%s.%s\n", $9, $5) }'`

    sleep $SLEEP_TIME

    FILE_SIZE_2=`ls -l $FILE | awk '{ printf("%s.%s\n", $9, $5) }'`

    if [ $FILE_SIZE_1 = $FILE_SIZE_2 ]; then

      SI_FILENAME=$FILE

      echo "  > Putting file $SI_PATH/$SI_FILENAME to Queue $QUEUE" >> $TMP_LOGFILE

        #----------------------------------------------------------------------------------

        # D E T E R M I N E   S P O O L I N    O P T I O N

        #----------------------------------------------------------------------------------

        SI_CMD="si(si-id=$QUEUE, si-filename=$SI_FILENAME); end; "

        DPBAT_RC=0

        echo $SI_CMD | $MHQ_HOME/dpbat >> $TMP_LOGFILE 2>&1

        DPBAT_RC=$?

        echo "Return Code (RC) DPBAT = $DPBAT_RC" >> $TMP_LOGFILE

        if [ $DPBAT_RC = "0" ]

        then

          #----------------------------------------------------------------------------

          # if RC=0 the spool-in was succesful, move to archive directory

          #----------------------------------------------------------------------------

          echo "--> File $SI_PATH/$SI_FILENAME successfully put to Queue $QUEUE." >> $TMP_LOGFILE

                  mv $SI_PATH/$SI_FILENAME $ARCHIVE_PATH

                  echo "--> File $SI_PATH/$SI_FILENAME moved to archive directory. \n " >> $TMP_LOGFILE

        else

          #----------------------------------------------------------------------------

          # if RC<>0 the spool-in was NOT succesful, move to error directory

          #----------------------------------------------------------------------------

          SCRIPT_RC=`expr $DPBAT_RC + $RC_DPBAT_ERR`  # some dpbat errors found!

          echo "--> Ended without putting file to Queue. (RC DPBAT: $DPBAT_RC)" >> $TMP_LOGFILE

          ERROR_MSG=$ERROR_MSG"\nEnded without putting file to Queue. (RC DPBAT: $DPBAT_RC)"

                  mv $SI_PATH/$SI_FILENAME $ERROR_PATH

          echo "--> Moved file to error directory $ERROR_PATH." >> $TMP_LOGFILE

          ERROR_MSG=$ERROR_MSG"\nMoved file to error directory $ERROR_PATH."

        fi

      fi

    fi

  done

else

echo "File $FILE does not exists" >> $TMP_LOGFILE

SCRIPT_RC=15

fi



#----------------------------------------------------------------------------------

# Analyze global error return code and send email if necessary

#----------------------------------------------------------------------------------

if [ $SCRIPT_RC -ne "0" ]

then

  echo "ERROR occured. (RC SCRIPT: $SCRIPT_RC)" >> $TMP_LOGFILE

  ERROR_MSG=$ERROR_MSG"\nERROR occurred. (RC SCRIPT: $SCRIPT_RC)"

  if [ $ERROR_NOTIFICATION = "y" ]

    then

      echo "Notification sent to $EMAIL_ADDRESS" >> $TMP_LOGFILE

      ERROR_MSG=$ERROR_MSG"\nNotification sent to $EMAIL_ADDRESS"

      error_mail

  fi

fi



#----------------------------------------------------------------------------------

# Append temporary log to main log and delete temporary log file

#----------------------------------------------------------------------------------

cat $TMP_LOGFILE >> $LOG_FILE_OUT

rm -f $TMP_LOGFILE

cd $ORG_PWD

return $SCRIPT_RC

答案1

是的,该return语句只能用于从 shell 函数或点脚本(使用.(dot) 或source在支持 别名的 shell 中获取的脚本.)返回退出状态。 Shell 函数和点脚本在这方面非常相似。

您显示的代码包含一个return语句作为其最后一个可执行语句,如果您将脚本作为普通脚本运行,这可能是引发错误的原因。

您的脚本很可能应该执行为

. ./script.sh

而不是作为

./script.sh

或者那个其他脚本应该做到这一点(这是未知的)。如果您确实使用点命令(或source例如)运行脚本,请注意,您将在脚本执行结束后bash留在其中(因为这是脚本在退出时更改工作目录的位置)。/ERQ/100/DM/SCRIPTS

另请注意,该脚本使用一些奇怪的代码来执行普通的操作。例如,要测试是否$ARCHIVE_PATH已定义

VARCHECK=`echo $ARCHIVE_PATH | wc -w`
if [ $VARCHECK = "0" ]

此测试不会测试它是否未定义,而是测试它是否为空(或仅包含 中的字符$IFS)。事实上,一半以上的脚本都由验证配置设置的语句占用。

相反,人们可以使用

if [ "${ARCHIVE_PATH-is unset}" = "is unset" ]

或者,在bash

if [ ! -v ARCHIVE_PATH ]

或者,如果您只想测试空值,

if [ -z "$ARCHIVE_PATH" ]

该脚本还在ls多个地方使用来获取文件列表,这不太理想。

相关内容