Execute Shell script after other script got execut

2019-07-03 02:12发布

Problem Statement:-

I have four shell script that I want to execute only when the previous script got executed successfully. And I am running it like this currently-

./verify-export-realtime.sh

sh -x lca_query.sh

sh -x liv_query.sh

sh -x lqu_query.sh

So In order to make other scripts run after previous script was successful. I need to do something like below? I am not sure whether I am right? If any script got failed due to any reason it will print as Failed due to some reason right?

./verify-export-realtime.sh

RET_VAL_STATUS=$?
echo $RET_VAL_STATUS
if [ $RET_VAL_STATUS -ne 0 ]; then
echo "Failed due to some reason"
exit
fi

sh -x lca_query.sh

RET_VAL_STATUS=$?
echo $RET_VAL_STATUS
if [ $RET_VAL_STATUS -ne 0 ]; then
echo "Failed due to some reason"
exit
fi

sh -x liv_query.sh

RET_VAL_STATUS=$?
echo $RET_VAL_STATUS
if [ $RET_VAL_STATUS -ne 0 ]; then
echo "Failed due to some reason"
exit
fi


sh -x lqu_query.sh

6条回答
▲ chillily
2楼-- · 2019-07-03 02:23

if you want more flexible of handling errors

script1.sh
rc=$?
if [ ${rc} -eq 0 ];then
echo "script1 pass, starting script2"
  script2.sh
  rc=$?
  if [ ${rc} -eq 0 ];then
   echo "script2 pass"
  else
   echo "script2 failed"
  fi
else
 echo "script1 failed"
fi
查看更多
Juvenile、少年°
3楼-- · 2019-07-03 02:27

That should be right. You can also print the error code if necessary by echoing the $ variable. You can also make your own return value codes by actually returning your own values in those scripts and checking them in this main one. It might be more helpful then "The script failed for some reason".

查看更多
太酷不给撩
4楼-- · 2019-07-03 02:30

The shell provides an operator && to do exactly this. So you could write:

./verify-export-realtime.sh && \
sh -x lca_query.sh && \
sh -x liv_query.sh && \
sh -x lqu_query.sh

or you could get rid of the line continuations (\) and write it all on one line

./verify-export-realtime.sh && sh -x lca_query.sh && sh -x liv_query.sh && sh -x lqu_query.sh

If you want to know how far it got, you can add extra commands that just set a variable:

done=0
./verify-export-realtime.sh && done=1 &&
sh -x lca_query.sh && done=2 &&
sh -x liv_query.sh && done=3 &&
sh -x lqu_query.sh && done=4

The value of $done at the end tells you how many commands completed successfully. $? will get set to the exit value of the last command run (which is the one that failed), or 0 if all succeeded

查看更多
看我几分像从前
5楼-- · 2019-07-03 02:34

As @William Pursell said, your scripts really should report their own errors. If you also need error reporting in the calling script, the easiest way to do it is like this:

if ! ./verify-export-realtime.sh; then
    echo "Error running verify-export-realtime.sh; rest of script cancelled." >&2

elif ! sh -x lca_query.sh; then
    echo "Error running lca_query.sh; rest of script cancelled." >&2

elif ! sh -x liv_query.sh; then
    echo "Error running liv_query.sh; rest of script cancelled." >&2

elif ! sh -x lqu_query.sh; then
    echo "Error running lqu_query.sh." >&2
fi
查看更多
成全新的幸福
6楼-- · 2019-07-03 02:36

You can simply run a chain of scripts in the command line (or from other script), when the first failing command will break this chain, using "&&" operator:

$ script1.sh && echo "First done, running the second" && script2.sh && echo "Second done, running the third" && script3.sh && echo "Third done, cool!"

And so on. The operation will break once one of the steps fails.

查看更多
我命由我不由天
7楼-- · 2019-07-03 02:39

The standard way to do this is to simply add a shell option that causes the script to abort if any simple command fails. Simply write the interpreter line as:

#!/bin/sh -e

or add the command:

set -e

(It is also common to do cmd1 && cmd2 && cmd3 as mentioned in other solutions.)

You absolutely should not attempt to print an error message. The command should print a relevant error message before it exits if it encounters an error. If the commands are not well behaved and do not write useful error messages, you should fix them rather than trying to guess what error they encountered. If you do write an error message, at the very least write it to the correct place. Errors belong on stderr:

echo "Some error occurred" >&2
查看更多
登录 后发表回答