0

I have a script which calls another script.

Now even if the second script fails it's still succeeding original script.

Below is the first part of the script:

#OBJ_TYP=`echo $Job_detail_corr  | awk -F'|' '{ print $7 }'`
case "$OBJ_TYP" in
"WF")

        echo "running workflows"
        . $SCRIPTS_PATH/Schedule_Wkflw.sh $DOM_NM $INT_SVC_NM $OBJ_NM $OS_PF_NM $LOG_PATH >> $LOG 2>&1
        RET_CODE=$?

        echo " RETURN CODE from status check is " $RET_CODE  >> $LOG 2>&1
         if [[ $RET_CODE -eq 0 ]]
        then
        echo "******************Completed execution of run_workflow*******************" >> $LOG 2>&1
        exit 0
        elif [[ $RET_CODE -eq 1 ]]
        then
         cho "******************Error executing run_workflow*******************" >> $LOG 2>&1
             exit 1
        else
             exit 1
            fi
 ;;

below is the schedule_wkflw.sh

./infacmd.sh wfs startworkflow -dn $DOM_NM  -un controlm -sn $INT_SVC_NM -a Application_$WF_NM -wf $WF_NM -osp $OS_PROFILE_NM -w true > $LOG_PATH/workflow.log.`date +%Y%m%d%H%M%S`

case $? in
0)
   echo "****** Workflow started successfully.******";
   exit 0;;
*)
   echo "****** Error starting workflow. ******";
   exit 1;;
esac

Now the problem even is if the schedule_wkflw.sh fails to execute correctly the parent script is giving exit 0.

I think the return code of parent script execution

. $SCRIPTS_PATH/Schedule_Wkflw.sh $DOM_NM $INT_SVC_NM $OBJ_NM $OS_PF_NM $LOG_PATH >> $LOG

is always giving 0, and that's the reason it's coming is Exit 0.

Can anyone please suggest a way to handle this correctly?

  • Do you get the expected log output in your logfile? The point here is that with the . $SCRIPTS_PATH/Schedule_Wkflw.sh you source the script, which is different from executing it. – AdminBee Feb 19 '20 at 10:04
  • as AdminBee mentioned, sourcing is different from a script call, so it always success, giving Exit 0 for you.... Why you are looking to source the script, @amit bhartiya? – Vishnu Kumar Feb 19 '20 at 10:35
  • @AdminBee Yes i am getting the expected log files. Will that always give a return code as 0? – Amit Feb 19 '20 at 11:40
  • @VishnuKumar we have some global variables defined in a parameter file which we want to use in the script probably because of that. Again, will that always give return code as 0? – Amit Feb 19 '20 at 11:42
0

I don't trust that "case $?" in the second script. I suspect the case itself changes $?.

Take a tip from the first script: RET_CODE=$? before you do anything else.

Do you get to see either of the echoes from that script? Does it say "Error starting workflow" before it apparently succeeds in the first script?

Hey, you are not executing Schedule_Wkflw.sh. There is a "." on that line in script 1. You are sourcing it. So it does not run as a separate process, and it does not even return an error code. When Schedule_Wkflw.sh hits exit, it exits the first script immediately because it is in the same shell.

So you won't be getting either of the messages out of script 1. Do you think you see those? The last echo runs "cho" so that's not going to happen anyway.

Almost every arg to everything is unquoted, which is never a good thing. Especially $SCRIPTS_PATH.

Why do you check RET_CODE for 0 and 1, and then wimp out without a message for any other value?

There is another logged direct debug of $RET_CODE. What does that say happened?

If you have that much debug built in, you really ought to be able to see what happens.

  • Do you get to see either of the echoes from that script? Does it say "Error starting workflow" before it apparently succeeds in the first script?-- Yes it does. – Amit Feb 19 '20 at 11:43
  • So you won't be getting either of the messages out of script 1. Do you think you see those?-- No i don't see those. What should i do here in order to get the expected code from the child process? – Amit Feb 19 '20 at 11:47
  • Read the other comments. There is no child process. What you think is the child script is included inline, because the "." command sources it. – Paul_Pedant Feb 19 '20 at 11:50
  • Yes, sorry it won't be a child process here as i am sourcing it. Shall i execute it in order to get the correct return code when the schedule_wkflw.sh exits with either 0 or 1? – Amit Feb 19 '20 at 11:58
  • That's the way it works. Exit ends a process, so the parent/child relationship has to exist so the parent does not get exited from the inline code. – Paul_Pedant Feb 19 '20 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.