Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.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
在zsh上的函数中的egrep上获取“无此类文件或目录”错误_Grep_Zsh - Fatal编程技术网

在zsh上的函数中的egrep上获取“无此类文件或目录”错误

在zsh上的函数中的egrep上获取“无此类文件或目录”错误,grep,zsh,Grep,Zsh,我创建了一个小函数,允许我在zsh上浏览命令历史记录。命令history 1将显示整个命令历史记录。运行history 1 | egrep ls只显示那些包含ls的命令 我的函数如下所示: h() { if [ -z "$*" ] then history 1 else history 1 | egrep "$@" fi } 不幸的是,这只会导致以下错误消息: $ h ls egrep: ls: No such file or directory 我不知道我的

我创建了一个小函数,允许我在zsh上浏览命令历史记录。命令
history 1
将显示整个命令历史记录。运行
history 1 | egrep ls
只显示那些包含
ls
的命令

我的函数如下所示:

h() {
  if [ -z "$*" ]
  then
    history 1
  else
    history 1 | egrep "$@"
  fi
}
不幸的是,这只会导致以下错误消息:

$ h ls
egrep: ls: No such file or directory

我不知道我的剧本出了什么问题。我尝试了
grep
egrep
都没有用。

grep或
egrep
的完整路径是什么

它可能运行在另一个shell中,该shell具有不同的
路径集。尝试使用显式的
/usr/bin/grep
/usr/bin/egrep
并查看是否修复了任何问题。

创建一个文件history.zsh(与原始文件略有更改):

现在源文件(因此“h”将被刷新):

并调用新函数:

$ h ls
30      h ls
31      ls

我已经放弃了这个功能。对zsh历史主题的进一步阅读使我找到了这个非常优雅的解决方案,满足了我的需求

简而言之,您可以将以下内容添加到.zshrc中:

autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-searc
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey "^[[A" up-line-or-beginning-search # Up
bindkey "^[[B" down-line-or-beginning-search # Down

现在,您可以通过输入部分术语并使用向上或向下箭头键来搜索历史记录文件中的匹配项。

哪个egrep
哪个grep
分别导致
/usr/bin/egrep
/usr/bin/grep
。在“我的函数”中拼写出其中一个的完整路径不会更改结果。仍在获取“没有这样的目录文件”错误消息。如果我向输入中添加其他参数,例如
hls1
,我将得到
egrep:1:没有这样的文件或目录
。您是否可能在幕后向shell传递其他信息?如果能够解释为什么从
history 1
中删除
1
,这个答案将非常有用。以及为什么在
egrep
上使用
fgrep
。历史记录1仅显示最新的历史记录项。我猜你想搜索所有的历史。fgrep比grep和egrep更严格,因为您正在寻找字符串,所以它本身就足够了(尽管grep和egrep也应该可以工作)
history
,例如,with no
1
只显示我的zsh安装上的最后15条命令<代码>历史记录1
使列表以第一个命令开始。更改为
fgrep
似乎没有什么不同。您还将
fgrep
“$@”
更改为
“$*”
。这消除了错误消息,但我没有得到任何输出。所有这些都让我觉得这个论点没有得到正确的通过。
$ h ls
30      h ls
31      ls
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-searc
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey "^[[A" up-line-or-beginning-search # Up
bindkey "^[[B" down-line-or-beginning-search # Down