如果我想让find缩写为
find dir_name -type d
转到fd
然后我就可以使用fd dir_name来执行命令了。
如何定义function或创建alias来完成此任务
如果我能这样做就更好了:fd dir-name other_operations,它等于
终端中的find dir_name -type d other_operations。
fish-shell内置文档中没有关于这方面的信息。
发布于 2014-09-25 13:30:33
你可以这样定义一个函数:
function fd
find $argv -type d
end函数的参数在$argv列表中传递。你可以自由地把它们切成小块,然后再把它们传给你去寻找。
发布于 2014-09-25 13:13:11
好吧,如果fish稍微遵循POSIX,那么像这样的函数就可以做到这一点。
fd() {
find "$@" -type d
}或者:
fd() {
dir="$1"
shift
find "$dir" -type d "$@"
}第一个假设所有参数都是可以在-type d之前的目录或操作数。第二个假设只有一个目录,然后是其他参数。
除了表示法的细节之外,您很可能还可以在fish中实现类似的东西。
当然,如果您访问http://fishshell.com/,特别是有关如何创建function的文档,您会发现语法上的相似性是有限的。
function fd
find $argv -type d
end
function fd
find $argv[1] -type d $argv[2..-1]
end最后一个函数只有在至少有2个参数传递给该函数时才起作用。这很奇怪;在其他地方不存在的变量扩展为零,但不是在像这样的数组扩展中。有一个(内置)命令count可以用来确定一个数组中有多少个元素:count $argv将返回数组中元素的数量。
因此,代码的修订版本为:
function fd
if test (count $argv) -gt 1
find $argv[1] -type d $argv[2..-1]
else
find $argv[1] -type d
end
end发布于 2014-09-25 15:02:33
感谢@Jonathan Leffler,这离不开他的帮助:
至于他的回答,$argv[2..-1](或$argv[2...-1])的最后一部分是不正确的,似乎fish-shell不支持这种语法,它说:
Could not expand string “$argv[2..-1]实际上是经过几次测试后,发现这部分是不必要的,如果$argv是一个列表,fish-shell将自动解析$argv的其余部分。
正确的模板是(已经测试过的,非常简单):
function fd --description 'List all the (sub)directory names in a direction'
find $argv[1] -type d
endhttps://stackoverflow.com/questions/26030865
复制相似问题