Shell 当 SQL 查询失败时脚本循环文件退出
Shell Script Loop over files to exit when SQL query fails
我们有一个 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 脚本以在第一个错误时退出脚本(但要注意 caveats)。
我们有一个 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 脚本以在第一个错误时退出脚本(但要注意 caveats)。