Bash 中的嵌套函数调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16325548/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:19:06  来源:igfitidea点击:

Nested function calls in Bash

bashfunction

提问by Anderson Green

Right now, I'm trying to nest one bash function call inside another function call (so that the output of one function is used as the input for another function). Is it possible to nest function calls in bash, as I'm trying to do here?

现在,我正在尝试将一个 bash 函数调用嵌套在另一个函数调用中(以便将一个函数的输出用作另一个函数的输入)。是否可以像我在这里尝试那样在 bash 中嵌套函数调用?

First, I defined these two functions:

首先,我定义了这两个函数:

returnSomething()
{
    return 5;
}

funky ()
{
  echo ;
}

Then, I tried to use the output of one function as the input for the other function. However, this next statement doesn't print the output of returnSomething. Instead, it prints nothing at all.

然后,我尝试使用一个函数的输出作为另一个函数的输入。但是,下一条语句不会打印returnSomething. 相反,它根本不打印任何内容。

funky $returnSomething; #Now I'm trying to use the output of returnSomething as the input for funky.

回答by ruakh

You have two problems. One is that returndoes not set the outputof a function, but rather its exit status(zero for success, nonzero for failure). For example, echo foowill outputfoo(plus a newline), but has an exit statusof 0. To control output, use echoor printf:

你有两个问题。一种是return不设置函数的输出,而是设置其退出状态(成功为零,失败为非零)。例如,echo foo输出foo(加上换行符),但退出状态0. 要控制输出,请使用echoprintf

function returnSomething ()     # should actually be outputSomething
{
    echo 5
}

The other problem is that $returnSomething(or ${returnSomething}) gives the value of a variable named returnSomething:

另一个问题是$returnSomething(或${returnSomething}) 给出了一个名为 的变量的值returnSomething

x=5          # sets the variable x
echo "$x"    # outputs 5

To capture the output of a command, use the notation $(...)(or `...`, but the latter is trickier). So:

要捕获命令的输出,请使用符号$(...)(或`...`,但后者更棘手)。所以:

function funky ()
{
    echo "$( "" )"
}
funky returnSomething    # prints 5

or just:

要不就:

function funky ()
{
    ""          # runs argument as a command
}
funky returnSomething    # prints 5

By contrast, if you do want to capture the exit status of a command, use the special shell parameter ?(which is set to the exit status of a command when it completes):

相比之下,如果您确实想捕获命令的退出状态,请使用特殊的 shell 参数?(设置为命令完成时的退出状态):

function returnSomething ()
{
    return 5
}
function funky ()
{
    ""          # runs argument as a command
    echo "$?"     # prints its exit status
}
funky returnSomething    # prints 5

回答by Kevin

Bash substitutes $(command)with the output of command, so you can use funky $(returnSomething)to pass funkythe output of returnSomething. However, it looks like you're misunderstanding returnin bash; the return value is a number that indicates whether the call was successful. The way to get the return status of the last process in bash is $?:

Bash 替换$(command)为 的输出command,因此您可以使用funky $(returnSomething)来传递funky的输出returnSomething。但是,您似乎return对 bash有误解;返回值是一个数字,表示调用是否成功。在bash中获取最后一个进程的返回状态的方法是$?

returnSomething
funky $?

回答by Hai Vu

How about this:

这个怎么样:

function foo() { echo one two three; }
function bar() { echo foo #1:  }
bar $( foo ) # ==> foo #1: one

The above has problem, thanks to Anderson for pointing it out. Here is an update:

上面有问题,感谢安德森指出。这是一个更新:

function foo() { echo one two three; }
function bar() { echo foo ; }
bar $( foo )