shell 脚本中的匿名函数
是否可以创建类似于匿名函数的东西,其值可以分配给数组元素并稍后调用?我似乎找不到在 bash 脚本中执行此操作的方法,但也许有解决方法。
Is it possible to create something analogous to an anonymous function whose value can be assigned to an array element and later called? I can't seem to find a way to do this in a bash script but perhaps there's a workaround.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
简短回答:不。
详细回答:不。
完整答案:bash 中的函数不是一流对象,因此 bash 中不可能存在匿名函数之类的东西。
Short answer: No.
Long answer: Nooooooooooooo.
Complete answer: Functions in bash are not first-class objects, therefore there can be no such thing as an anonymous function in bash.
这是可能的;我写了一个库来完成这个任务,尽管这是一个非常奇怪的项目。源代码位于 http://github.com/spencertipping/bash-lambda。使用此库:
技巧是让
fn
函数创建一个包含函数主体的文件,chmod +x
该文件,然后返回其名称。这会导致杂散文件累积,这就是为什么该库还实现了异步标记/清除垃圾收集器。It is possible; I wrote a library to do exactly this, though it's a very strange project. The source code is available at http://github.com/spencertipping/bash-lambda. Using this library:
The trick is to have the
fn
function create a file containing the body of the function,chmod +x
that file, then return its name. This causes stray files to accumulate, which is why the library also implements an asynchronous mark/sweep garbage collector.常见的技术是有条件地分配函数定义:
The common technique is to assign function definitions conditionally:
如果您确实需要数组来存储函数,您可以定义命名函数并仅存储它们的名称。然后您可以将函数调用为
${array[n]}
。或者,您可以将它们命名为func1
..funcN
,然后调用func$n
。If you really need array to store the functions, you can define named functions and store just their names. You can then call the function as
${array[n]}
. Or, you can name themfunc1
..funcN
and then just callfunc$n
.bash 是图灵完整的,所以这是完全可能的;)
但除此之外,它并不真正值得考虑。
你可以用类似的方法来模拟这种行为:
但是为什么?!?
well bash is turing complete, soo thats perfectly possible ;)
but aside from this its not really worth the consideration.
you could simulate such behaviour though with something along this line:
but why?!?
我遇到这个问题是因为我正在寻找如何做类似的事情
,即能够以灵活的方式操纵管道中的先前结果。
xargs
我听说过在命令行上处理此问题的一个好方法是通过 xargs 命令。我的第一印象是它不是很灵活
默认情况下它将标准输入通过任何空格分割成单独的参数,然后将这些参数提供给命令(下面的示例会更清楚)
我刚刚了解到来自这非常有帮助gist Brainiarc7 写了 xargs 如何变得真正灵活和有用。 一定要阅读/浏览一下,它有更多解释,真的很好。我在下面提供了示例和我从中收集到的内容,希望对您有所帮助。
1.
-n
:将参数拆分为n
大小的块使用
-n 1
可能非常有用2.
-I
:使用占位符将参数插入命令中一个示例可能最能说明其工作原理。
您提供
-I
要用作占位符的字符串。占位符可以是您喜欢的任何文本(例如my_unique_placeholder
),但%
是常规的。3.
-0
:通过空字符(UTF+0000)对args进行分块这不太方便,但是您可以通过
tr
使用它来按任意字符进行分割。4.
-L
:将n
行上的参数分割成一个块https://gist.github.com/Brainiarc7/133fd582e124981c08cbafca98455ee9
https://shapeshed.com/unix-xargs/
I came across this question because I was looking for how to do something analogous to
i.e. Being able to manipulate previous results in a pipeline in a flexible way.
xargs
A nice way I had heard of to handle this on the command line is through the
xargs
command. My first impression was it wasn't very flexibleBy default it splits standard input by any whitespace into separate arguments, then feeds those arguments to the command (it'll be clearer with the examples below)
I just learned from this extremely helpful gist Brainiarc7 wrote how xargs can be really flexible and useful. Definitely read/skim that, it's really good with more explanation. I included examples and things I gleaned from it below though, hopefully it's helpful.
1.
-n
: Split up args inton
-sized chunksUsing
-n 1
will probably be very useful2.
-I
: Interpolate args into a command using a placeholderAn example probably best illustrates how this one works.
You give
-I
the string you want to use as a placeholder. The placeholder can be whatever text you like (e.g.my_unique_placeholder
), but%
is conventional.3.
-0
: Chunk args by the null character (UTF+0000)This isn't as convenient, but you can use it to split by an arbitrary character via
tr
.4.
-L
: split args onn
lines into a chunkhttps://gist.github.com/Brainiarc7/133fd582e124981c08cbafca98455ee9
https://shapeshed.com/unix-xargs/
在您的
PATH
中创建 fn 文件,然后您可以执行以下操作:
Create the fn file in your
PATH
You can then do :