顺便说一句:使用| cut -D: -f1
并不安全,因为grep的不逃避在文件名中的冒号。看看我的意思,你可以试试这个:
[email protected]:~$ echo bar:baz > foo
[email protected]:~$ echo baz > foo:bar
[email protected]:~$ grep -Hr ba .
./foo:bar:baz
./foo:bar:baz
明确..没有。
所以...让我们澄清你在找什么。
- 是否要在多个文件中搜索一个字符串?或者,
- 是否要在一个文件中搜索多个字符串?
如果是前者,那么下面可能的工作:
#!/bin/bash
if [[ "$#" -lt 2 ]]; then
echo "Usage: `basename $0` string file [file ...]
exit 1
fi
what="$1"
shift # discard $1, move $2 to $1, $3 to $2, etc.
for where in "[email protected]"; do
grep -HlR "$what" "$where" -R
done
而如果是后者,那么这将是这样:
#!/bin/bash
if [[ "$#" -lt 2 ]]; then
echo "Usage: `basename $0` file string [string ...]
exit 1
fi
where="$1"
shift
for what in "[email protected]"; do
grep -lR "$what" "$where"
done
当然,这其中可能会被精简如果你用一个或者一个条连接你的字符串,然后使用egrep。取决于你实际寻找的东西。
那么你想访问传递给脚本的命令行参数吗? – 2013-02-09 22:42:58
是的,谢谢;我似乎无法表达我在做什么。例如,我希望能够将第一个参数传递给第一个变量位置,第二个等等。 – 2013-02-09 22:44:18