我正在编写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.我的错误在哪里?
答案 0 :(得分: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