bash 中的 lambda 函数

发布于 2024-07-12 02:14:37 字数 117 浏览 4 评论 0原文

有没有办法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

何以笙箫默 2024-07-19 02:14:37

我不知道有什么方法可以做到这一点,但是您也许可以使用以下方法来完成您想要做的事情:

somecommand | while read -r; do echo "Something with $REPLY"; done

这也会更快,因为您不会为每一行文本创建一个新进程。

[编辑2009-07-09]
我做了两处更改:

  1. 合并了 litb 的建议,即使用 -r 禁用反斜杠处理——这意味着输入中的反斜杠将原封不动地传递。
  2. 我们让 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:

somecommand | while read -r; do echo "Something with $REPLY"; done

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:

  1. Incorporated litb's suggestion of using -r to disable backslash processing -- this means that backslashes in the input will be passed through unchanged.
  2. Instead of supplying a variable name (such as X) as a parameter to read, we let read 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 running read X (e.g., with the command IFS='' 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.

深居我梦 2024-07-19 02:14:37

如果你想要真正的函数,而不仅仅是管道或 while 循环(例如,如果你想传递它们,就好像它们是数据一样),我只是不做 lambda,并使用重复的虚拟名称定义虚拟函数,以使用正确的方法离开,然后扔掉。 就像这样:

# An example map function, to use in the example below.
map() { local f="$1"; shift; for i in "$@"; do "$f" "$i"; done; }

# Lambda function [λ], passed to the map function.
λ(){ echo "Lambda sees $1"; }; map λ *

就像在正确的函数式语言中一样,不需要传递参数,因为您可以将它们包装在闭包中:

# Let’s say you have a function with three parameters
# that you want to use as a lambda:
# (As in: Partial function application.)
trio(){ echo "$1 Lambda sees $3 $2"; }

# And there are two values that you want to use to parametrize a
# function that shall be your lambda.
pre="<<<"
post=">>>"

# Then you’d just wrap them in a closure, and be done with it:
λ(){ trio "$pre" "$post" "$@"; }; map λ *

我认为它比这里提出的所有其他解决方案还要短。

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:

# An example map function, to use in the example below.
map() { local f="$1"; shift; for i in "$@"; do "$f" "$i"; done; }

# Lambda function [λ], passed to the map function.
λ(){ echo "Lambda sees $1"; }; map λ *

Like in proper functional languages, there’s no need to pass parameters, as you can wrap them in a closure:

# Let’s say you have a function with three parameters
# that you want to use as a lambda:
# (As in: Partial function application.)
trio(){ echo "$1 Lambda sees $3 $2"; }

# And there are two values that you want to use to parametrize a
# function that shall be your lambda.
pre="<<<"
post=">>>"

# Then you’d just wrap them in a closure, and be done with it:
λ(){ trio "$pre" "$post" "$@"; }; map λ *

I’d argue that it’s even shorter than all other solutions presented here.

始终不够爱げ你 2024-07-19 02:14:37

那这个呢?

somecommand | xargs -d"\n" -I{} echo "the argument is: {}"

(假设每个参数都是一行,否则更改分隔符)

What about this?

somecommand | xargs -d"\n" -I{} echo "the argument is: {}"

(assumes each argument is a line, otherwise change delimiter)

过潦 2024-07-19 02:14:37
#!/bin/bash

function customFunction() {
    eval $1
}

command='echo Hello World; echo Welcome;'
customFunction "$command"

GL

来源

#!/bin/bash

function customFunction() {
    eval $1
}

command='echo Hello World; echo Welcome;'
customFunction "$command"

GL

Source

一影成城 2024-07-19 02:14:37

如果您只需要 xargs(例如,由于并行 -P N 选项),并且只需要 bash 作为函数代码,那么 bash -c 可以用作 xargs 的参数。

seq 1 10 | tr '\n' '\0' | xargs -0 -n 1 bash -c 'echo any bash code $0'

此处使用 tr 和 -0 选项来禁用任何 xargs 参数替换。

if you want only xargs (due parallel -P N option for example), and only bash as function code, then bash -c can be used as parameter for xargs.

seq 1 10 | tr '\n' '\0' | xargs -0 -n 1 bash -c 'echo any bash code $0'

tr and -0 option are used here to disable any xargs parameters substitutions.

独夜无伴 2024-07-19 02:14:37

是的。 可以传递一个表示命令调用的字符串变量,然后使用 eval 执行该命令。

例子:

command='echo howdy'
eval "$command"

Yes. One can pass around a string variable representing a command call, and then execute the command with eval.

Example:

command='echo howdy'
eval "$command"
流星番茄 2024-07-19 02:14:37

eval 技巧已经被提到过,但这是我的 bash 闭包的扩展示例:

#!/usr/bin/env bash

set -e

function multiplyBy() {
    X="$1"

    cat <<-EOF
        Y="\$1"
        echo "$X * \$Y = \$(( $X * \$Y ))"
    EOF
}

function callFunc() {
    CODE="$1"
    shift

    eval "$CODE"
}

MULT_BY_2=`multiplyBy 2`
MULT_BY_4=`multiplyBy 4`

callFunc "$MULT_BY_2" 10
callFunc "$MULT_BY_4" 10

PS 我只是为了完全不同的目的而想出这个,只是在 google 上搜索看看某人是否正在使用它。 我实际上需要在主脚本的上下文(shell)中评估可重用的函数。

The eval trick has been already mentioned but here's my extended example of bash closures:

#!/usr/bin/env bash

set -e

function multiplyBy() {
    X="$1"

    cat <<-EOF
        Y="\$1"
        echo "$X * \$Y = \$(( $X * \$Y ))"
    EOF
}

function callFunc() {
    CODE="$1"
    shift

    eval "$CODE"
}

MULT_BY_2=`multiplyBy 2`
MULT_BY_4=`multiplyBy 4`

callFunc "$MULT_BY_2" 10
callFunc "$MULT_BY_4" 10

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文