bash 其他脚本执行成功后执行Shell脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12902371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:33:01  来源:igfitidea点击:

Execute Shell script after other script got executed successfully

linuxbashshellunix

提问by arsenal

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-

我有四个 shell 脚本,我只想在前一个脚本成功执行时执行。我目前正在像这样运行它-

./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 reasonright?

所以为了让其他脚本在之前的脚本成功后运行。我需要做类似下面的事情吗?我不确定我是否正确?如果任何脚本由于任何原因失败,它会由于某种原因打印为失败,对吗?

./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

回答by Chris Dodd

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

shell 提供了一个操作符&&来执行此操作。所以你可以写:

./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 $doneat 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 0if all succeeded

最后的值$done告诉你有多少命令成功完成。 $?将被设置为最后一个命令运行的退出值(这是失败的那个),或者0如果一切都成功

回答by kliteyn

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.

等等。一旦其中一个步骤失败,操作就会中断。

回答by phani

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

回答by William Pursell

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:

执行此操作的标准方法是简单地添加一个 shell 选项,如果任何简单命令失败,该选项将导致脚本中止。只需将解释器行写为:

#!/bin/sh -e

or add the command:

或添加命令:

set -e

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

cmd1 && cmd2 && cmd3其他解决方案中提到的做法也很常见。)

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:

您绝对不应该尝试打印错误消息。如果遇到错误,该命令应该在退出之前打印相关的错误消息。如果命令表现不佳并且没有编写有用的错误消息,您应该修复它们而不是试图猜测它们遇到了什么错误。如果你确实写了一条错误信息,至少把它写到正确的地方。错误属于 stderr:

echo "Some error occurred" >&2

回答by Gordon Davisson

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

正如@William Pursell 所说,您的脚本确实应该报告自己的错误。如果您需要在调用脚本中报告错误,最简单的方法是这样的:

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

回答by Florin Stingaciu

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".

那应该是对的。如有必要,您还可以通过回显 $ 变量来打印错误代码。您还可以通过在这些脚本中实际返回您自己的值并在此主要脚本中检查它们来制作自己的返回值代码。这可能比“脚本由于某种原因失败”更有帮助。