I am trying to do something in a bash script whenever a file in a directory I am iterating over contains a string using grep
. The problem comes in where a subset of the files in the directory contain spaces in the name. Therefore, I have tried to replace the spaces with escaped spaces in place using sed
:
if grep -c "main" ${source} | sed 's/ /\\ /g'; then
# do something
fi
However, I still get the error:
grep: /Users/me/Desktop/theDir/nameWith: No such file or directory
grep: spaces.txt: No such file or directory
What am I doing wrong?
Answer
You should quote the name of the file being grep'ed:
if grep -c main "$source" ; then
# do something
fi
...assuming $source
is the name of a file. If $source
is the name of a directory, I'll need more information about what you're trying to do.
No comments:
Post a Comment