Search code examples
bashif-statementconditional-statementsevaluate

evaluating a bash command with if


i am looking to create a delete query that is going to get sent to a database.

I really don't have a model for doing so, so I look for the key word delete in the /bigcompany/scripts/dbscripts/ directory and see how others have done it.

When i use this i get a whole bunch of good examples, but it does not give me the names of the scripts :

cat /bigcompany/scripts/dbscripts/* | grep delete

I would like to know the name of the script that has the delete line query, so I can see the query in the context of the script.

I tried :

for i in $(ls /bigcompany/scripts/dbscripts/ )
do 
if [ cat /bigcompany/scripts/dbscripts/$i | grep delete ] 
   then
   echo $i
   fi 
done  

I was thinking if "cat /bigcompany/scripts/dbscripts/ | grep delete" evaluated to successful, the script would echo the script name that contained the keyword.

However it does not.


Solution

  • You want a list of the file names containing matches, so use -l to do that (all in one fell swoop, which has the additional merit of avoiding problems with blanks and other oddball characters in the file names):

    grep -l delete /bigcompany/scripts/dbscripts/*
    

    If you're dealing with SQL, unless there's a strong standard to the contrary, you probably want to recognize DELETE too, so add -i (for case-insensitive):

    grep -l -i delete /bigcompany/scripts/dbscripts/*