I’m new to bash scripts (and the *nix shell altogether) but I’m trying to write this script to make grepping a codebase easier.
I have written this
#!/bin/bash
args=("$@");
for arg in args
grep arg * */* */*/* */*/*/* */*/*/*/*;
done
when I try to run it, this is what happens:
~/Work/richmond $ ./f.sh "\$_REQUEST\['a'\]" ./f.sh: line 4: syntax error near unexpected token `grep' ./f.sh: line 4: ` grep arg * */* */*/* */*/*/* */*/*/*/*;' ~/Work/richmond $
How do I do this properly?
And, I think a more important question is, how can I make grep recurse through subdirectories properly like this?
Any other tips and/or pitfalls with shell scripting and using bash in general would also be appreciated.
The syntax error is because you’re missing
do. As for searching recursively if yourgrephas the-Roption you would do:Otherwise you could use
find:In the latter example,
findwill executegrepand replace the{}braces with the file names it finds, starting in the current directory..(Notice that I also changed
argto"$arg". You need the dollar sign to get the variable’s value, and the quotes tell the shell to treat its value as one big word, even if$argcontains spaces or newlines.)