bash shell 脚本中的匿名函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8427955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Anonymous functions in shell scripts
提问by Matty
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.
是否可以创建类似于匿名函数的东西,其值可以分配给数组元素并在以后调用?我似乎找不到在 bash 脚本中执行此操作的方法,但也许有解决方法。
回答by Ignacio Vazquez-Abrams
Short answer: No.
简短的回答:没有。
Long answer: Nooooooooooooo.
长答案:Nooooooooooooo。
Complete answer: Functions in bash are not first-class objects, therefore there can be no such thing as an anonymous function in bash.
完整答案:bash 中的函数不是一等对象,因此 bash 中不存在匿名函数这样的东西。
回答by Spencer Tipping
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:
有可能的; 我写了一个库来做到这一点,尽管这是一个非常奇怪的项目。源代码可从http://github.com/spencertipping/bash-lambda 获得。使用这个库:
$ my_array=()
$ my_array[0]=$(fn x 'echo $((x + 1))')
$ my_array[1]=$(fn x 'echo $((x + 2))')
$ ${my_array[0]} 5
6
$ ${my_array[1]} 5
7
$
The trick is to have the fnfunction create a file containing the body of the function, chmod +xthat file, then return its name. This causes stray files to accumulate, which is why the library also implements an asynchronous mark/sweep garbage collector.
诀窍是让fn函数创建一个包含函数主体的chmod +x文件,该文件,然后返回其名称。这会导致杂散文件堆积,这就是该库还实现异步标记/清除垃圾收集器的原因。
回答by choroba
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 them func1.. funcNand then just call func$n.
如果确实需要数组来存储函数,则可以定义命名函数并仅存储它们的名称。然后,您可以将该函数调用为${array[n]}. 或者,您可以将它们命名为func1..funcN然后只需调用func$n.
回答by William Pursell
The common technique is to assign function definitions conditionally:
常用的技术是有条件地分配函数定义:
#!/bin/sh
case in
a) foo() { echo case a; };;
b) foo() { echo case b; };;
*) foo() { echo default; } ;;
esac
foo
回答by edi9999
Create the fn file in your PATH
在您的文件中创建 fn 文件 PATH
#!/bin/sh
printusage () {
printf "Create anonymous function, for example\n"
printf "fn 'echo " "'"
exit 1
}
[ "$#" = "1" ] || printusage
fun=
[ "$fun" = "" ] && printusage
fun_file="$(mktemp /tmp/fun_XXXXXX)"
echo "#!/bin/sh" > "$fun_file"
echo "" >> "$fun_file"
echo "$fun" >> "$fun_file"
chmod u+x "$fun_file"
echo "$fun_file"
You can then do :
然后你可以这样做:
foo=$(fn 'echo ')
${foo} "bar"
回答by slaxor
well bash is turing complete, soo thats perfectly possible ;)
好吧,bash 是图灵完整的,所以这是完全可能的 ;)
but aside from this its not really worth the consideration.
但除此之外,它并不真正值得考虑。
you could simulate such behaviour though with something along this line:
您可以通过以下方式模拟此类行为:
echo myval ; ( foocmd "$_" && barcmd "$_" )
but why?!?
但为什么?!?

