View Full Version : If "file contains ..." then "..." else "..." fi

July 23rd, 2008, 05:16 PM
I am very lost here, its something I would have expected to be easy to do! Or at least find out how to do :|

So say I want to search a specific file for STRING and execute a bash command if it exists in the file. What is the command for this?

Thanks, Ant

July 23rd, 2008, 05:28 PM
Here's one way:

grep STRING file -q && echo 'Found it!'


$ grep "main" main.cpp -q && echo 'Found it!'
Found it!
$ grep "xxxyyyzzz" main.cpp -q && echo 'Found it!'

Replace the echo command with the command to be run if the string is found.

July 23rd, 2008, 05:51 PM
Thanks, that's perfect!