4

问题陈述:-

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

./verify-export-realtime.sh

sh -x lca_query.sh

sh -x liv_query.sh

sh -x lqu_query.sh

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

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

6 回答 6

7

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

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

或者您可以去掉续行 ( \) 并将其全部写在一行上

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

如果你想知道它有多远,你可以添加额外的命令来设置一个变量:

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

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

于 2012-10-15T19:18:10.727 回答
2

您可以简单地在命令行(或从其他脚本)中运行一系列脚本,当第一个失败的命令将破坏该链时,使用“&&”运算符:

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

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

于 2012-10-15T19:19:12.833 回答
0

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
于 2012-10-15T22:08:13.357 回答
0

如果您想要更灵活地处理错误

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
于 2012-10-15T20:18:03.603 回答
0

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

于 2012-10-15T19:11:12.437 回答
0

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
于 2012-10-16T01:54:42.413 回答