问题描述
我试图做一个简单的bash脚本做的每个文件的一个东西在一组文件夹。此外,我想算脚本多少文件读取,但是当循环的脚本通,数值变量被复位。
I'm trying to do a simple bash script to do something in one of each file in a set of folders. Also I like to count how many files the script read, but when the script pass of the loop, the numerical variable is reseted.
在code我使用的就是这样
The code I'm using is like that
#!/bin/bash
let AUX=0
find . -type "f" -name "*.mp3" | while read FILE; do
### DO SOMETHING with $FILE###
let AUX=AUX+1
echo $AUX
done
echo $AUX
我可以看到,辅助线的内循环计数,但最后回声打印0,而变量似乎真的复位。我的控制台输出就是这样
I can see that AUX is counting inside the loop, but the last "echo" prints a 0, and the variable seems to be really reseted. My console output is like that
...
$ 865
$ 866
$ 867
$ 868
$ 0
我想preserve在AUX proccesed文件的数量。任何想法?
I would like to preserve in AUX the number of files proccesed. Any idea?
推荐答案
不要使用管道,它会创建一个子shell。下面的例子。
Do not use the pipe, it creates a subshell. Example below.
#!/bin/bash
declare -i AUX=0
while IFS='' read -r -d '' file; do
### DO SOMETHING with $file###
(( ++AUX ))
echo $AUX
done < <(find . -type "f" -name "*.mp3")
echo $AUX
这篇关于While循环在Bash脚本重新设置数字变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!