从bash脚本返回值 [英] return values from bash script
问题描述
我想创建一个返回值的Bash文件。
的意思是,在脚本script_a.bash中我有一定的计算能力,脚本script_b.bash会调用它。
I want to create a Bash file that returns a value. Meaning, in script script_a.bash I have a certain calculation, and script script_b.bash will call it.
script_a.bash:
script_a.bash:
return $1*5
script_b.bash:
script_b.bash:
a_value=./script_a.bash 3
当a_value变为15时。
when a_value will be 15.
我读了一点,发现bash中没有返回,但是可以使用 echo在函数中完成类似的操作。我不想使用函数,我需要在多个地方使用通用脚本。
I read a little bit about that, and saw that there's no "return" in bash, but something like this can be done in functions using "echo". I don't want to use functions, I need a generic script to use in multiple places.
是否可以从其他脚本返回值?谢谢!
Is it possible to return a value from a different script? Thanks!
推荐答案
使用命令替换来捕获 echo
的输出,并使用算术表达式计算结果:
Use command substitution to capture the output of echo
, and use arithmetic expression to count the result:
script_a.bash:
script_a.bash:
echo $(( $1 * 5 ))
script_b.bash
script_b.bash
a_value=$( script_a.bash 3 )
这篇关于从bash脚本返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!