我们有一个Shell脚本,如果任何一个.pl文件失败,该脚本将停止运行。如果SQL文件存在,我们希望对它们进行相同的处理。区别在于,在循环中的那个时刻我们并不总是有一个SQL文件。因此,我们并不总是有一个file_num.sql。我们想要做的是,如果它存在并且失败了,那么退出下面的脚本。我们采用这种结构的方式是在do_updates文件中这样运行:
my $sqlFile = $updatesdir.'/releaseupdates_02.sql';
system("$drushcmd -r $drupalroot -u site.test.admin sql-query --file=$sqlFile") == 0 or die "$drushcmd failed: $?";
因此,在下面的主循环中,如果包含SQL命令的perl脚本失败,我们想退出循环。因此,假设有一个perl脚本调用了3和4的php文件,并且如果出错,则可以使用,但是,如果存在02的SQL文件而不是php文件,则该脚本不起作用。
# Declare a list of update script file numbers
FILE_NUMS="
01
02
03
04
"
# Run each update script
for file_num in $FILE_NUMS; do
${RELEASEDIR}/do_updates_$file_num.pl
# Exit if an update script returns and exit status of anything other than zero
if [ $? -ne 0 ]; then
exit $?
fi
done
您exit $?
没有获得与上一行相同的退出代码。您可以如下简化:
${RELEASEDIR}/do_updates_$file_num.pl || exit $?
更好的是,通过添加set -o errexit
以在第一个错误时退出脚本来编写健壮的Shell脚本(但请注意警告)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句