如果没有差异,计算差异会使脚本退出
Counting diff makes the script exit if there is no diff
我尝试统计一些冲突的文件:
let initialConflicts=`git diff --name-only --diff-filter=U | wc -l`
问题是:如果没有冲突,git diff
的结果为空,wc -l
挂起。它似乎来自 git diff
或 wc -l
中缺少的选项。这个小脚本重现了这个问题:
set -euo pipefail
IFS=$'\n\t'
echo $SHELL
echo "Before counting"
let test=`echo -n "" | wc -l`
echo "After counting"
这里,“计数后”这一行没有出现。
请参阅 bash
手册页中的 let
:
Each arg is an arithmetic expression to be evaluated
...
If the last arg evaluates to 0, let
returns 1; 0 is returned otherwise.
正如您set -e
,这会导致脚本退出。由于您不在此处评估表达式,因此只需:
test=`echo -n "" | wc -l`
应该可以。
我尝试统计一些冲突的文件:
let initialConflicts=`git diff --name-only --diff-filter=U | wc -l`
问题是:如果没有冲突,git diff
的结果为空,wc -l
挂起。它似乎来自 git diff
或 wc -l
中缺少的选项。这个小脚本重现了这个问题:
set -euo pipefail
IFS=$'\n\t'
echo $SHELL
echo "Before counting"
let test=`echo -n "" | wc -l`
echo "After counting"
这里,“计数后”这一行没有出现。
请参阅 bash
手册页中的 let
:
Each arg is an arithmetic expression to be evaluated ... If the last arg evaluates to 0, let returns 1; 0 is returned otherwise.
正如您set -e
,这会导致脚本退出。由于您不在此处评估表达式,因此只需:
test=`echo -n "" | wc -l`
应该可以。