Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何构建自定义grep函数_Bash_Shell - Fatal编程技术网

Bash 如何构建自定义grep函数

Bash 如何构建自定义grep函数,bash,shell,Bash,Shell,我是bash新手,我正在尝试构建自己的grep别名来搜索文件 以下是我构建的脚本: function gia() { if [ -z "$1" ]; then echo "-You need to define what are you looking for-" else echo "-Looking for \"$1\".-" if [ -z $2 ]; then echo "-no path passed, searching all files

我是bash新手,我正在尝试构建自己的grep别名来搜索文件 以下是我构建的脚本:

function gia() {
  if [ -z "$1" ]; then
    echo "-You need to define what are you looking for-"
  else
    echo "-Looking for \"$1\".-"
    if [ -z $2 ]; then
      echo "-no path passed, searching all files-"
      grep -i --color "$1" ./*
    else
      echo "-Looking in \"$2\".-"
      grep -i --color "$1" $2
    fi
  fi
}
第二个选项效果不好,如果我尝试,我会得到以下输出:

$$ gia 'sometext' ./*.html
-Looking for "sometext".-
-Looking in "./login.html".-
我从未指定过login.html,但它获取了我目录中的一个文件并在其中进行了搜索。grep失败了

例如,如果我在myfiles目录中有3个文件:

1.html2.txt3.html

html有文本“反引号”

如果我这样搜索:

cd myfiles
gia 'backquotes'
gia 'backquotes' ~/myfiles/*.html
我得到了结果

-Looking for "backquotes".-
-no path passed, searching all files-
./3.html:    <backquotes>...</backquotes>
我明白了:

-Looking for "backquotes".-
-Looking in "./1.html".-
没有返回结果,因为它只在1.html中搜索。如果我在1.html中有“反引号”。它将返回,但我没有从其他文件中得到任何信息,它只在第一个文件中搜索并退出

我知道明星是bash中的一个特殊角色,但我如何解决这个问题呢

提前谢谢你的帮助


EMMNS

如果要对函数使用“通配符”文件名,则必须解析$。这是函数的参数列表。我暂时不在这个解决方案中解析路径

function gia() {
  if [ -z "$1" ]; then
    echo "-You need to define what are you looking for-"
  else
      for f in $@
      do
         grep -i --color "$f" ./*
      done
    fi
  fi
}

如果要对函数使用“通配符”文件名,则必须对$@进行分析。这是函数的参数列表。我暂时不在这个解决方案中解析路径

function gia() {
  if [ -z "$1" ]; then
    echo "-You need to define what are you looking for-"
  else
      for f in $@
      do
         grep -i --color "$f" ./*
      done
    fi
  fi
}

您可以使用
${@:2}
符号拼接
$@
中从第二个开始的所有参数。同时引用
“${@:2}”
,因为文件名可以包含空格和任何需要转义的特殊字符

这应该起作用:

function gia() {
if [ -z "$1" ]; then
    echo "-You need to define what are you looking for-"
else
    echo "-Looking for \"$1\".-"
    if [ -z $2 ]; then
        echo "-no path passed, searching all files-"
        grep -i --color "$1" ./*
    else
        echo "-Looking in \"${@:2}\".-"
        grep -i --color "$1" "${@:2}"
    fi
fi
}
输出:

$ gia ABC file*.html
-Looking for "ABC".-
-Looking in "file1.html file 2.html file 3.html file4.html file5.html".-
file 2.html:ABC
file 3.html:ABC
file5.html:ABC

您可以使用
${@:2}
符号拼接
$@
中从第二个开始的所有参数。同时引用
“${@:2}”
,因为文件名可以包含空格和任何需要转义的特殊字符

这应该起作用:

function gia() {
if [ -z "$1" ]; then
    echo "-You need to define what are you looking for-"
else
    echo "-Looking for \"$1\".-"
    if [ -z $2 ]; then
        echo "-no path passed, searching all files-"
        grep -i --color "$1" ./*
    else
        echo "-Looking in \"${@:2}\".-"
        grep -i --color "$1" "${@:2}"
    fi
fi
}
输出:

$ gia ABC file*.html
-Looking for "ABC".-
-Looking in "file1.html file 2.html file 3.html file4.html file5.html".-
file 2.html:ABC
file 3.html:ABC
file5.html:ABC

向我们展示您如何调用
gia
。从外观上看,
$2
似乎具有值
/login.html
或者,您可以使用
查找$dir\u name-name“$file\u name\u pattern”-exec grep-i--color=auto“$string\u to_grep”{}向我们展示如何调用
gia
。从外观上看,
$2
似乎具有值
/login.html
或者,您可以使用
查找$dir\u name-name“$file\u name\u pattern”-exec grep-i--color=auto“$string\u to_grep”{}是的,我明白,我的问题是:当我指定路径时,grep失败,并且我没有返回结果,我将用更多的解释来编辑我的帖子。我现在理解thks-尝试在*.html周围使用单引号。我没有,没有返回任何结果:(另一个解决方案-希望这对您更有用。根据您编写的代码,这将搜索当前目录中所有文件中的$f文本…我希望能够在文本中搜索我传递的特定路径,而不是。/*,,,我喜欢for循环,我将使用它传递多个文本,但仍然无法解决我的问题。)路径问题…是的,我理解,我的问题是:当我指定路径时,grep失败,并且我没有返回结果,我将使用更多的解释来编辑我的帖子。我现在理解thks-尝试在*.html周围使用单引号。我曾经,没有返回任何结果:(另一个解决方案-希望这对您更有用。根据您编写的代码,这将搜索当前目录中所有文件中的$f文本…我希望能够在文本中搜索我传递的特定路径,而不是。/*,,,我喜欢for循环,我将使用它传递多个文本,但仍然无法解决我的问题。)路径问题…太棒了,这就是我需要的,谢谢@Tuxdude,但我想我会删除第二条echo语句,因为如果我有300个文件(就像我现在的情况一样);页面上充满了文件名,,,有点烦人…但它确实起到了作用太棒了,这是我需要的,谢谢@Tuxdude,但我想我会删除第二个echo语句,因为如果我有300个文件(就像我现在的情况);页面上充满了文件名,,,有点烦人…但它确实起到了作用