bash 从 shell 脚本返回一个值,到另一个 shell 脚本中

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

return a value from shell script, into another shell script

bashshell

提问by John1024

I know how to return an exit code, but I would like to return the result of an operation done in a shell script function, so I can eventually use it in another script or function.

我知道如何返回退出代码,但我想返回在 shell 脚本函数中完成的操作的结果,以便我最终可以在另一个脚本或函数中使用它。

Something like

就像是

var1=$(myfunction)

function2 var1

Where myfunction could be something like A+B=C

myfunction 可能类似于 A+B=C

I looked into "return", but it will return a code, not a value.

我查看了“返回”,但它会返回一个代码,而不是一个值。

I am looking into various sites that show how to write functions, but I don't see how you actually return values.

我正在查看展示如何编写函数的各种站点,但我没有看到您实际上是如何返回值的。

In C++ you would use return "variable name", but shell script won't allow this. It says that the variable do not exist (which is logical, it is a variable created in a function, so when the function is released, that memory space assigned to it is gone). Can't use global variables since the function may be in one script and the calling function that needs the return value, may be in a different one.

在 C++ 中,您将使用 return "variable name",但 shell 脚本不允许这样做。它表示该变量不存在(这是合乎逻辑的,它是在函数中创建的变量,因此当函数被释放时,分配给它的内存空间就消失了)。不能使用全局变量,因为函数可能在一个脚本中,而需要返回值的调用函数可能在不同的脚本中。

回答by John1024

myfunction could be something like A+B=C

myfunction 可能类似于 A+B=C

Just echo the result:

只需回显结果:

$ myfunction() { echo $((+)); }

The above myfunctionadds two numbers and echoes the result.

以上myfunction将两个数字相加并回显结果。

The return value can then be captured just as you had it:

然后可以像您一样捕获返回值:

$ var=$(myfunction 12 5)
$ echo $var
17

The construct var=$(myfunction)captures the standard out from myfunctionand saves it in var. Thus, when you want to return something from myfunction, just send it to standard, like we did with echoin the example above.

该构造var=$(myfunction)从 中捕获标准myfunction并将其保存在var. 因此,当您想从 返回某些内容时myfunction,只需将其发送到标准,就像我们echo在上面的示例中所做的那样。

In cases where you want the return value to be carefully formatted, you should consider using printfin place of echo.

在您希望仔细格式化返回值的情况下,您应该考虑使用printf代替 echo。

More: How to return multiple values

更多:如何返回多个值

Let's define a function that produces two outputs:

让我们定义一个产生两个输出的函数:

$ f() { echo "output1" ; echo "output2" ; }
$ f
output1
output2

If you want to get those values back separately, the most reliable method is to use bash's arrays:

如果你想分别取回这些值,最可靠的方法是使用 bash 的数组:

$ a=($(f))

The above executes f, via $(f)and saves the results in an array called a. We can see what is in aby using declare -p:

上面执行f, via$(f)并将结果保存在一个名为 的数组中a。我们可以a使用declare -p以下命令查看其中的内容:

$ declare -p a
declare -a a='([0]="output1" [1]="output2")'

回答by Slakjaw

I use the same sorta thing for returning values from other scripts to my main script like the title suggests.

我使用相同的东西将值从其他脚本返回到我的主脚本,如标题所示。

At the end of the 2nd script, I echo the variable I want to return to the main script:

在第二个脚本的末尾,我回显了我想返回到主脚本的变量:

#!/bin/bash
# This is the Second Script.

# Store the variables passed from the main script:
VAR1_FROM_MAIN_SCRIPT=
VAR2_FROM_MAIN_SCRIPT=

# Add the 2 variables and store as another variable to return:
RETURN_THIS=$(($VAR1_FROM_MAIN_SCRIPT + VAR2_FROM_MAIN_SCRIPT)) 

# This is the variable you are sending back to the main script:
echo "$RETURN_THIS"    #<---- This won't print to screen!!!

Then in the main script I pass in a couple variables to, and execute, the 2nd script like this:

然后在主脚本中,我将几个变量传递给并执行第二个脚本,如下所示:

#!/bin/bash
# This is the Main Script.

PASS_VAR1_TO_SCRIPT=1
PASS_VAR2_TO_SCRIPT=2

# Call the second script and store it's results in this variable:
RETURN_VARIABLE=$(./secondScriptName "$PASS_VAR1_TO_SCRIPT" "$PASS_VAR2_TO_SCRIPT")

# Display the returned variable from the second script:
echo $RETURN_VARIABLE    #<---- Will display 3

The reason the echo in the second script won't print to screen, is because it's running that second script in a subshell from the RETURN_VARIABLE... I know my explanation of the subshell sucks, but that's besides the point...
Also, I know you can source the other script, but this might help others.

第二个脚本中的 echo 不会打印到屏幕的原因是因为它在 RETURN_VARIABLE 的子shell 中运行第二个脚本......我知道我对子shell 的解释很糟糕,但这不是重点......
另外,我知道您可以获取其他脚本,但这可能对其他人有所帮助。

回答by anubhava

In shell scripting you don't return a value but just echo (print) it and caller would capture the output of your script/function to grab the returned value.

在 shell 脚本中,您不返回值,而只是回显(打印)它,调用者将捕获脚本/函数的输出以获取返回值。

Example:

例子:

dateval=$(date)
echo $dateval
Wed Apr 23 18:35:45 EDT 2014

Instead of dateyou can place your function or your shell script.

而不是date你可以放置你的函数或你的shell脚本。