What am I doing wrong?
Several things that are rather common bugs when you are "new to Bash".
In echo \"$line\"
the double-quotes are escaped, not special, they get to the output and $line
is unquoted, which is by itself wrong. You actually see the quotes in the file. Then they get stored in $line
(at the second read
). And then this happens: quotes in variables are not special.
A basic fix is to replace echo \"$line\"
with echo "$line"
. This won't make the script good and robust, but at least it will work in most cases.
There is more to improve:
There should be a shebang.
-name "*"
infind
is a test that always succeeds.Use
IFS= read -r line
also with the firstread
. Or better get rid of thisread
because…Reading lines and storing in a file, only to read lines from the file to process further seems over-complicated. If you really want to process pathnames this way, pipe from
grep
directly to the secondwhile
.You don't even need this
grep
. Append! \( -name '*.git*' -prune \)
to your firstfind
and get rid ofgrep
.This line:
find "$line" -type f -name "*" #| wc -l <<<a | echo "$line : $((a-1))"
contains commented code that looks like a remnant of some voodoo scripting. I guess at some point this was not a comment, just weird code. In particular:
wc -l <<<a
reads the here string (literallya
) from its stdin and there is no point in piping anything to it, the here string will "win".- There is no point in piping to
echo
, it does not read its stdin anyway.
Some ideas about counting files: here.