-1

Im trying to learn some bash script writing and cleaning out some old CDs (remember those?)

What I have is a bunch of backup CDs with no rhyme or reason to them so I'd like to create a grep script that will search them based on keywords. (I'm currently trying to test the script first against the desktop) I know how to run grep, but its the scripting I'm having trouble with.

So here's what I have:

 #!/bin/bash
 #CDGrepScript

 SOURCEDIR=/home/name/Desktop (I'm currently testing it with files on the desktop)
 #mount /dev/cdrom     
 #SOURCEDIR=/dev/cdrom

 echo "hello, $USER. I will search your files"
 echo Begin Search...

 grep -ir "taxes|personal|School" * 

 echo $results
 echo "The search is complete. Goodbye"

 exit

Now when I run this against files on the desktop. My script hangs after "Begin search" What am I doing wrong?

Thanks for the help

4

1 に答える 1

1

より一般的なツールを使用すると、より適切に機能する場合があります。ツリーをたどって検索語を検索する rgrep (再帰的な grep) のように。例:

# rgrep
#
# Search for text strings in a directory hierarchy
set +x
case $# in
0 | 1 )
       # Not enough arguments -- give help message
       echo "Usage: $0 search_text pathname..." >&2
       exit 1
       ;;
* )
       # Use the first argument as a search string
       search_text=$1
       shift
       # Use the remaining argument(s) as path name(s)
       find "$@" -type f -print |
       while read pathname
       do
        egrep -i "$search_text" $pathname /dev/null
       done
       ;;
esac

これをパスに入れてから、ディレクトリを CD-ROM のマウント ポイントに変更し、次のように入力します。

$ rgrep "taxes" .  

または、実行したいその他の検索。

于 2013-03-14T18:47:00.933 に答える