使用多个参数查找和 grep

Using multiple arguments to find and grep

我正在尝试制作一个脚本,以便我可以使用可变数量的参数 myfind one two three 命令,它会找到文件夹中的所有文件,然后应用 grep -i one 然后 grep -i two , 和 grep -i three 等等。

我尝试了以下代码:

#! /bin/bash
FULLARG="find . | "
for arg in "$@"
do 
    FULLARG=$FULLARG" grep -i "$arg" | "
done
echo $FULLARG
$FULLARG

但是,虽然创建了命令,但它不工作并给出以下错误:

$ ./myfind one  two three
find . | grep -i one | grep -i two | grep -i three |
find: unknown predicate `-i'

问题出在哪里,如何解决?

您可以存储 find . 的结果并继续过滤直到您有命令行参数:

#!/bin/bash

result="$(find .)"
for arg in "$@"
do
    result=$(echo "$result" | grep -i "${arg}")
done

echo "$result"