我的脚本遇到问题。我的变量未加1。 我的循环遍历目录中的每个文件。
#!/bin/bash -x
files=0
find $PWD -type f -print0 | while IFS= read -r -d '' file; do
files=$((files+1))
echo "Files should be +1 now."
done
echo $files
结果:
+ files=0
+ IFS=
+ read -r -d '' file
+ find /home/home/Documents/sh -type f -print0
+ files=1
+ echo 'Files should be +1 now.'
Files should be +1 now.
+ IFS=
+ read -r -d '' file
+ files=2
+ echo 'Files should be +1 now.'
Files should be +1 now.
+ IFS=
+ read -r -d '' file
+ files=3
+ echo 'Files should be +1 now.'
Files should be +1 now.
+ IFS=
+ read -r -d '' file
+ echo 0
0
这是怎么回事?您可以在输出中清楚地看到files变量正在增加吗?那么它怎么仍然打印0?
编辑: 我知道现在管道会创建一个新的子外壳,并且不会影响父子外壳。我该如何解决?
matches=0
find $workingDir -type f -print0 | while IFS= read -r -d '' filePath1; do
find $workingDir -type f -print0 | while IFS= read -r -d '' filePath2; do
# some calculation done on filepath1 and filepath 2
# if calculation result is equal then increase matches + 1
done
done