bash 中的 lambda 函数
有没有办法在 bash 中实现/使用 lambda 函数? 我在想这样的事情:
$ someCommand | xargs -L1 (lambda function)
Is there a way to implement/use lambda functions in bash? I'm thinking of something like:
$ someCommand | xargs -L1 (lambda function)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我不知道有什么方法可以做到这一点,但是您也许可以使用以下方法来完成您想要做的事情:
这也会更快,因为您不会为每一行文本创建一个新进程。
[编辑2009-07-09]
我做了两处更改:
-r
禁用反斜杠处理——这意味着输入中的反斜杠将原封不动地传递。read
分配给它的默认变量,而不是提供变量名称(例如
。 这具有保留前导空格和尾随空格的令人愉快的副作用,否则这些空格将被剥离(即使内部空格被保留)。X
)作为read
的参数。回复根据我的观察,这些更改共同保留了每个输入行上除文字 NUL (ASCII 0) 字符之外的所有内容。
[EDIT 26/7/2016]
根据评论者 Evi1M4chine 的说法,在运行
read X
之前将$IFS
设置为空字符串(例如,使用命令IFS='' read X
) 在将结果存储到$X
时还应保留开头和结尾的空格,这意味着您不必强制使用$回复
。I don't know of a way to do this, however you may be able to accomplish what you're trying to do using:
This will also be faster, as you won't be creating a new process for each line of text.
[EDIT 2009-07-09]
I've made two changes:
-r
to disable backslash processing -- this means that backslashes in the input will be passed through unchanged.X
) as a parameter toread
, we letread
assign to its default variable,REPLY
. This has the pleasant side-effect of preserving leading and trailing spaces, which are stripped otherwise (even though internal spaces are preserved).From my observations, together these changes preserve everything except literal NUL (ASCII 0) characters on each input line.
[EDIT 26/7/2016]
According to commenter Evi1M4chine, setting
$IFS
to the empty string before runningread X
(e.g., with the commandIFS='' read X
) should also preserve spaces at the beginning and end when storing the result into$X
, meaning you aren't forced to use$REPLY
.如果你想要真正的函数,而不仅仅是管道或 while 循环(例如,如果你想传递它们,就好像它们是数据一样),我只是不做 lambda,并使用重复的虚拟名称定义虚拟函数,以使用正确的方法离开,然后扔掉。 就像这样:
就像在正确的函数式语言中一样,不需要传递参数,因为您可以将它们包装在闭包中:
我认为它比这里提出的所有其他解决方案还要短。
if you want true functions, and not just pipes or while loops (e.g. if you want to pass them around, as if they were data) I’d just not do lambdas, and define dummy functions with a recurring dummy name, to use right away, and throw away afterwards. Like so:
Like in proper functional languages, there’s no need to pass parameters, as you can wrap them in a closure:
I’d argue that it’s even shorter than all other solutions presented here.
那这个呢?
(假设每个参数都是一行,否则更改分隔符)
What about this?
(assumes each argument is a line, otherwise change delimiter)
GL
来源
GL
Source
如果您只需要 xargs(例如,由于并行
-P N
选项),并且只需要 bash 作为函数代码,那么bash -c
可以用作 xargs 的参数。此处使用 tr 和 -0 选项来禁用任何 xargs 参数替换。
if you want only xargs (due parallel
-P N
option for example), and only bash as function code, thenbash -c
can be used as parameter for xargs.tr and -0 option are used here to disable any xargs parameters substitutions.
是的。 可以传递一个表示命令调用的字符串变量,然后使用 eval 执行该命令。
例子:
Yes. One can pass around a string variable representing a command call, and then execute the command with eval.
Example:
eval 技巧已经被提到过,但这是我的 bash 闭包的扩展示例:
PS 我只是为了完全不同的目的而想出这个,只是在 google 上搜索看看某人是否正在使用它。 我实际上需要在主脚本的上下文(shell)中评估可重用的函数。
The eval trick has been already mentioned but here's my extended example of bash closures:
PS I've just came up with this for a completely different purpose and was just searching google to see if sb is using that. I actually needed to evaluate a reusable function in the context (shell) of main script.