-1

我正在尝试学习一些 bash 脚本编写和清理一些旧 CD(还记得那些吗?)

我拥有的是一堆没有押韵或理由的备份 CD,因此我想创建一个 grep 脚本,该脚本将根据关键字搜索它们。(我目前正在尝试首先在桌面上测试脚本)我知道如何运行 grep,但它是我遇到问题的脚本。

所以这就是我所拥有的:

 #!/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

现在,当我对桌面上的文件运行它时。我的脚本在“开始搜索”后挂起我做错了什么?

谢谢您的帮助

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 回答