0

在谷歌群组中问过这个问题,但我看到以前在这里问过类似的问题,所以我也在这里问过。这似乎是一个范围界定问题,但据我所知,该变量是全局的。

统计的退出代码始终为零。

谢谢。

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;"
4

2 回答 2

0

while循环在子外壳中运行,因此变量操作对父级不可见。

这是一个常见问题解答。 http://mywiki.wooledge.org/BashFAQ/024针对 Bash 用户,但通常也适用于kshPOSIX shell。

于 2013-09-04T14:38:54.800 回答
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
于 2013-09-04T14:49:36.630 回答