順便說一句:使用| 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