Professional Web Applications Themes

Simple bash script to grep files for bad keywords - FreeBSD

Hello, I am trying to write a simple bash script that will grep all files in a directory (except ones that start with "00") for certain bad keywords. Here is what I have so far: #!/bin/bash # This is a simple script to check all sql scripts for bad keywords BAD_KEYWORDS='spool echo timing commit rollback' for i in $BAD_KEYWORDS; do echo "*********************************"; echo "GREPing for bad keyword '$i'" echo "*********************************"; grep $i ./*; done However, I'm not sure how to make it not grep the files that start with "00". Can anyone help me with this? Thanks /Brian...

  1. #1

    Default Simple bash script to grep files for bad keywords

    Hello,
    I am trying to write a simple bash script that will grep all files in a
    directory (except ones that start with "00") for certain bad keywords.
    Here is what I have so far:
    #!/bin/bash

    # This is a simple script to check all sql scripts for bad keywords

    BAD_KEYWORDS='spool echo timing commit rollback'

    for i in $BAD_KEYWORDS;
    do
    echo "*********************************";
    echo "GREPing for bad keyword '$i'"
    echo "*********************************";
    grep $i ./*;
    done

    However, I'm not sure how to make it not grep the files that start with
    "00". Can anyone help me with this?

    Thanks

    /Brian
    Brian Guest

  2. #2

    Default Re: Simple bash script to grep files for bad keywords

    On 2005-03-23 12:29, Brian John <com> wrote: 
     

    Use xargs, since it will buy you the extra feature of being able to
    search through arbitrarily large numbers of files:

    for _word in ${BAD_KEYWORDS} ;do
    find . | grep -v '^/00' |\
    xargs grep "${_word}" /dev/null
    done

    Tips:

    - The quotes in "${_word}" are probably optional, but it's better to
    be safe than sorry :-)

    - The /dev/null is there so that grep will get at least 2 file
    arguments, even if there is just one file in the current directory,
    effectively forcing grep(1) to print the filename of this one file
    if it happens to match the pattern.

    Giorgos Guest

  3. #3

    Default Re: Simple bash script to grep files for bad keywords

    > On 2005-03-23 12:29, Brian John <com> wrote: 

    >
    > Use xargs, since it will buy you the extra feature of being able to
    > search through arbitrarily large numbers of files:
    >
    > for _word in ${BAD_KEYWORDS} ;do
    > find . | grep -v '^/00' |\
    > xargs grep "${_word}" /dev/null
    > done
    >
    > Tips:
    >
    > - The quotes in "${_word}" are probably optional, but it's better to
    > be safe than sorry :-)
    >
    > - The /dev/null is there so that grep will get at least 2 file
    > arguments, even if there is just one file in the current directory,
    > effectively forcing grep(1) to print the filename of this one file
    > if it happens to match the pattern.
    >[/ref]
    Cool, I think I get it for the most part. However, what exactly am I
    doing when I am piping to xargs? I can see that the filenames not
    starting with '00' will be piped, but what does the '\' do? Sorry, I am
    really new to scripting and *nix in general. But I am a programmer so I
    learn fast.

    Thanks!

    /Brian
    Brian Guest

  4. #4

    Default Re: Simple bash script to grep files for bad keywords

    Brian John wrote:
     
    >>Use xargs, since it will buy you the extra feature of being able to
    >>search through arbitrarily large numbers of files:
    >>
    >> for _word in ${BAD_KEYWORDS} ;do
    >> find . | grep -v '^/00' |\
    >> xargs grep "${_word}" /dev/null
    >> done
    >>
    >>Tips:
    >>
    >>- The quotes in "${_word}" are probably optional, but it's better to
    >> be safe than sorry :-)
    >>
    >>- The /dev/null is there so that grep will get at least 2 file
    >> arguments, even if there is just one file in the current directory,
    >> effectively forcing grep(1) to print the filename of this one file
    >> if it happens to match the pattern.
    >>
    >>
    >>[/ref]
    >Cool, I think I get it for the most part. However, what exactly am I
    >doing when I am piping to xargs? I can see that the filenames not
    >starting with '00' will be piped, but what does the '\' do? Sorry, I am
    >really new to scripting and *nix in general. But I am a programmer so I
    >learn fast.
    >
    >Thanks!
    >
    >/Brian
    >_______________________________________________
    >org mailing list
    >http://lists.freebsd.org/mailman/listinfo/freebsd-questions
    >To unsubscribe, send any mail to "org"
    >
    >
    >I can see that the filenames not
    >starting with '00' will be piped, but what does the '\' do?[/ref]
    The '\' means pretend I typed the next line on this line
    he used it because most mailers wrap the e-mail lines at 80 characters,
    but it needs to be on one line to work. If you wrote it on one line in the
    script file, you can omit that '\'

    Luck

    --
    END
    -----------------------------------------------------------------------------
    Philip M. Gollucci
    Senior Developer - Liquidity Services Inc.
    Phone: 202.568.6268 (Direct)
    E-Mail: com
    Web: http://www.liquidation.com

    Philip Guest

Similar Threads

  1. php+bash script
    By mateusz kula in forum PHP Development
    Replies: 0
    Last Post: October 22nd, 06:30 PM
  2. Replies: 4
    Last Post: November 28th, 05:28 PM
  3. bash script help
    By another2 in forum UNIX Programming
    Replies: 1
    Last Post: July 28th, 07:06 AM
  4. bash: can I cd from a script
    By Colin Watson in forum Debian
    Replies: 3
    Last Post: July 24th, 11:30 AM
  5. simple bash loop problem ...
    By Jonathan Matthews in forum Debian
    Replies: 13
    Last Post: June 30th, 10:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139