在谷歌小组中问这个问题,但我看到之前在这里问过的类似问题,所以我也在这里问。 这似乎是一个范围问题,但据我所知,变量是全局的。
Tallied退出代码始终为零。
感谢。
ksh -c "exit_code=0;
# Get a list of sql files and interate through them to do certain work on a DB.
find ./sql_email_reports -maxdepth 1 -type f -print | while read line;
do echo \"Report = \" \${line};
#Now do some sql work on a DB based on the sql file as given by $line.
#and if the work to the DB fails for some reason, send back a return code greater than zero.
rc=\$?;
# Test the incrementation of exit_code by setting rc, which should increment exit_code for every file found in directory.
rc=1
echo \"rc = \" \${rc};
(( exit_code+=rc ));
echo \"Exit Code =\" \${exit_code};
done;
enter code here
# For some reason, the tallied exit_code is not what it is within the while loop, it is still zero
echo \"Tallied exit_code = \" \${exit_code};
(( exit_code > 0 )) && exit 1;
exit 0;"
答案 0 :(得分:0)
while
循环在子shell中运行,因此变量操作对父级不可见。
这是常见问题解答。 http://mywiki.wooledge.org/BashFAQ/024针对的是Bash用户,但通常也适用于ksh
和POSIX shell。
答案 1 :(得分:0)
您可以使用glob而不是find
来消除管道:
exit_code=0
for f in ./sql_email_reports/*; do
[ -f "$f" ] || continue # -type f
f=${f##*/} # Strip leading path, leaving the base name
rc=1
echo "rc=$rc"
(( exit_code += rc ))
echo "Exit Code = $exit_code"
done
echo "Tallied exit_code = $exit_code"
(( exit_code > 0 )) && exit 1
exit 0