0

我正在编写 git hook 并且对下一个代码行为感到非常困惑:

#!/bin/sh

exit_code=0

git diff --cached --name-only --diff-filter=ACM | while read line; do
    echo "Do something with file: $line"
    # some stuff with exit code is equals to 0 or 1
    stuff_exit_code=$?
    exit_code=$(($exit_code + $stuff_exit_code))
done

echo $exit_code
exit $exit_code

我希望echo $exit_code将为每个我的东西退出代码非零生成文件总量。但我总是看到 0。我的错误在哪里?

4

1 回答 1

0

这是因为管道在不同的进程中执行。只是将其替换为for-in循环。

#!/bin/sh

exit_code=0

for file in `git diff --cached --name-only --diff-filter=ACM`
do
    echo "Do something with file: $file"
    # some stuff with exit code is equals to 0 or 1
    stuff_exit_code=$?
    exit_code=$(($exit_code + $stuff_exit_code))
done

echo $exit_code
exit $exit_code
于 2013-07-18T08:16:05.697 回答