hi all, im working on a script that needs to read in a file, search for a string of text within that file, and if the string is found, do something. so i know i need to use an if-then statement, but how do i test for the existence of a string within the file? any help would be greatly appreciated.
Try:
if $(grep -q PATTERN ${1})
here's my hack code:
-bash-3.00$ cat testfile | if $(grep -q Something ${1}); then echo "it's there"; fi
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .
looks like it doesnt like the q. im using solaris 10 here. i did a man grep and the q option does show up, im not sure why its failing here. i'm wanting to read in a file called testfile and look for the word "Something".
Don't use the cat command in that context, do :
if $(grep -q Something testfile); then echo "it's there"; fi
A shortcut :
grep -q Something testfile && echo "it's there"
The -q option works fine for me under bash on my PC and ksh on mi AIX box.
In the both cases, the path of the command is /usr/bin/grep
The which command will show you the path of the grep command.
$ which grep
/usr/bin/grep
$
Try to specify the full path for the grep command.
Jean-Pierre.
hey, hey, hey! that first example worked just fine when i used the right version of grep! lol thanks SOOO much for your help!!
The backticks -- aka $(command) -- are useless too, the following is quite sufficient.
if grep -q Something testfie; then ...
For the record, the -q option is quite trivial to replace; just redirect output to nowhere.
if grep Something testfile >/dev/null; then ...
thanks for all the info