Where is the error in this script please:
#!/bin/bash rep="git" files=`find' ${rep} '-type f` for f in ${files} do echo $f done
When i run find git -type f
alone in the shell, it works!
Strings in single quotes are not interpolated. It means, you are trying to run
find ' ${rep} '-type f
Remove the single quotes. If you really need to quote the $rep (e.g. because it contains spaces), use double quotes:
files=`find "$rep" -type f`
Note that there are no spaces inside the double quotes. You are searching 'git', not ' git ', right?
Don't store the output of find
in a variable. It's unreliable and inefficient. Inefficient, because find
has to finish running before you start processing its output; this makes a difference on a large directory tree. Unreliable because even if you got the command syntax right, the output of find
would consists of a list of file names separated by newlines. This is ambiguous unless the file names are known not to contain newlines. Furthermore, you aren't parsing the output correctly: with an unquoted command substitution $(…)
, the result of the command is broken into separate words wherever there's whitespace (not just newlines), and the words are interpreted as shell glob patterns. To avoid this processing, use "$(…)"
(always put double quotes around variable and command substitutions: "$foo"
, "$(foo)"
); this is no good here since you need to separate the file names.
find
has a built-in feature to process the resulting files: the -exec
action. Use it. That's what it's for.
find "$rep" -type f -exec echo {} \;
If you need to run an arbitrary shell snippet and not just a single command on each file, invoke a shell as the command. To avoid mangling the file name, pass it as a parameter to the shell.
find "$rep" -type f -exec sh -c ' echo "$0" # more shell commands ... ' {} \;
Remove quotes from
files=`find '$rep' -type f`
The correct script is
#!/bin/bash rep="git" files=`find $rep -type f` for f in ${files}; do echo $f done
IFS
characters.CommentedOct 10, 2012 at 10:53