gpt4 book ai didi

linux - 如何将别名命令与 xargs 一起使用?

转载 作者:IT王子 更新时间:2023-10-29 00:14:09 60 4
gpt4 key购买 nike

我的 .aliases 中有以下别名:

alias gi grep -i

我想在名称中包含字符串 bar 的所有文件中不区分大小写地查找 foo:

find -name \*bar\* | xargs gi foo

这是我得到的:

xargs: gi: No such file or directory

有什么方法可以在 xargs 中使用别名,还是必须使用完整版:

   find -name \*bar\* | xargs grep -i foo

注意:这是一个简单的例子。除了 gi 之外,我还有一些非常复杂的别名,我无法轻易地手动扩展这些别名。

编辑:我使用了 tcsh,所以请说明答案是否特定于 shell。

最佳答案

别名是特定于 shell 的——在这种情况下,很可能是特定于 bash 的。要执行别名,您需要执行 bash,但别名只会为交互式 shell 加载(更准确地说,.bashrc 只会为交互式 shell 读取)。

bash -i 运行交互式 shell(和 sources .bashrc)。bash -c cmd 运行 cmd

把它们放在一起:bash -ic cmd 在交互式 shell 中运行 cmd,其中 cmd 可以是定义在你的 .bashrc.

find -name \*bar\* | xargs bash -ic gi foo

应该做你想做的。

编辑:我看到您已将问题标记为“tcsh”,因此特定于 bash 的解决方案不适用。使用 tcsh,您不需要 -i,因为它似乎读取 .tcshrc,除非您提供 -f

试试这个:

find -name \*bar\* | xargs tcsh -c gi foo

它适用于我的基本测试。

关于linux - 如何将别名命令与 xargs 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/979453/

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com