0

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".

jwzumwalt
  • 275
  • 1
  • 8
  • 2
    [This](https://superuser.com/a/1348970/432690). Additionally `find … | while read …` is not the most robust solution, `find … -exec …` is almost always the right way. At least use [`IFS= read -r …`](https://unix.stackexchange.com/q/209123/108618). – Kamil Maciorowski Jun 30 '23 at 20:03
  • 1
    Note: the subshells are note "within the `find`". Everything but `find ./ -name whatever-$SEARCH-expands-to` is outside of `find`. – Kamil Maciorowski Jun 30 '23 at 20:46
  • 1
    A general way to count the results of `find`: [here](https://superuser.com/a/1687310/432690). In the context of where it says "`find` performs tests of our choice and prints tokens" note `-exec cp "$SOURCE" {} \;` may be a test. Please see [*Understanding the `-exec` option of `find`*](https://unix.stackexchange.com/q/389705/108618). – Kamil Maciorowski Jun 30 '23 at 20:53
  • Thanks Kamil, but your information assumes I know what your talking about on several levels that I do not understand. The same is true for the links. Kindly provide a complete example, then I will be able to add to my limited knowledge :-) – jwzumwalt Jul 01 '23 at 17:12
  • 1
    The first link gives you `shopt -s lastpipe`. Have you tried to run it at the beginning of your script? It should make your counting work. The script will remain kinda flawed, but if you "have used this script for several decades without any problems" then probably it's good enough for you. A sane and easy improvement is `IFS= read -r` from the second link, it will make the script less flawed. – Kamil Maciorowski Jul 01 '23 at 17:47
  • Yes I tried shopt -s lastpipe. "Good enough" is not good enough... I would like to know the number of replacements. – jwzumwalt Jul 01 '23 at 17:51
  • 1
    Try `shopt -s lastpipe` with your *original* script. – Kamil Maciorowski Jul 01 '23 at 17:52
  • Bingo!! that worked!!! – jwzumwalt Jul 01 '23 at 17:55
  • It's OK to add still-not-working tries to a question, but adding a solution invalidates the question as there is no longer a problem to be solved. Please remove the solution from your question. IMO it would be best if you decided if the question is specifically about your counter or if it's more general "how can I count the number of times `find` loops?". In the former case `shopt -s lastpipe` is a solution; the latter case begs for a solution that fixes all the flaws. – Kamil Maciorowski Jul 01 '23 at 18:05

0 Answers0