I have a bash file that does a global update to files with a given name. I have used this script for several decades without any problems. I became curious as to how many files were being updated and added a var that was incremented after each update. However, even though it is updating files the var "CNT" always reports no files where updated.
I am using the suggested "inc" from this website. What am I doing wrong?
Thanks for the help :-)
#!/usr/bin/bash
SEARCH=test.txt # file name to search for
SOURCE=test.txt # file name to replace it
CNT=0; # number of files updated
find ./ -name "$SEARCH" | while read f; do
cp "$SOURCE" "$f" > /dev/null 2>&1; # copy and don't show errors
((CNT=CNT+1))
done; # end of while
echo -en "\n"
echo -en "$CNT files where replaced.\n"
I also tried using this as suggested but did not find any difference regardless of inserting an "inc" or "wc" (word count) in various syntax.
shopt -s lastpipe
find . -name "$search" -type f -exec 'bash
for f; do
cp "$f" "$source"
done' sh {} +
Based on the link provided by Kamil Maciorowski (below), sub-shells are being created within the "find". Perhaps a better question is "How can I count the number of times find loops?"
Solution: Adding "shopt -s lastpipe" fixed the first script but the suggested 2nd script still did not work with "shopt -s lastpipe".