Linux shell 脚本函数返回一个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11758368/
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
shell script function return a string
提问by Lolly
I am new to shell scripts, I am trying to create a simple function which will return the concatenated two strings that are passed as parameters. I tried with below code
我是 shell 脚本的新手,我正在尝试创建一个简单的函数,该函数将返回作为参数传递的连接的两个字符串。我试过下面的代码
function getConcatenatedString() {
echo "String1 "
echo "String2 "
str=/
echo "Concatenated String ${str}"
echo "${str}"
}
//I am calling the above function
//我正在调用上面的函数
constr=$(getConcatenatedString "hello" "world")
echo "printing result"
echo "${constr}"
echo "exit"
I see the below output when running the script with above code,
使用上述代码运行脚本时,我看到以下输出,
printing result
String1 hello
String2 world
Concatenated String hello/world
hello/world
exit
If you look at the code I am first calling the function and then I am echoing "printing result" statement, but the result is first comes the "printing result" and echos the statement inside the function. Is the below statement calling the function
如果您查看代码,我首先调用函数,然后回显“打印结果”语句,但结果首先是“打印结果”并回显函数内部的语句。下面的语句是调用函数吗
constr=$(getConcatenatedString "hello" "world")
or
或者
echo ${constr}
is calling the function ?
正在调用函数?
Because if I comment out #echo ${constr} then nothing is getting echoed !!! Please clarify me.
因为如果我注释掉 #echo ${constr} 那么什么都不会得到回应!!!请澄清我。
采纳答案by paxdiablo
The first is calling the function and storing allof the output (four echo
statements) into $constr
.
第一个是调用函数并将所有输出(四个echo
语句)存储到$constr
.
Then, after return, you echo the preamble printing result
, $constr
(consisting of four lines) and the exit message.
然后,返回后,您回显 preamble printing result
, $constr
(由四行组成)和退出消息。
That's how $()
works, it captures the entire standard output from the enclosed command.
这就是$()
工作原理,它从包含的命令中捕获整个标准输出。
It sounds like you want to see some of the echo
statements on the console rather than capturing them with the $()
. I think you should just be able to send them to standard error for that:
听起来您想echo
在控制台上查看某些语句,而不是使用$()
. 我认为你应该能够将它们发送到标准错误:
echo "String1 " >&2
回答by William Pursell
paxdiablo's solution is correct. You cannot return a string from a function, but you can capture the output of the function or return an integer value that can be retrieved by the caller from $?
. However, since all shell variables are global, you can simply do:
paxdiablo 的解决方案是正确的。您不能从函数返回字符串,但可以捕获函数的输出或返回可由调用者从 中检索的整数值$?
。但是,由于所有 shell 变量都是全局变量,您可以简单地执行以下操作:
getConcatenatedString() { str="/"; }
getConcatenatedString hello world
echo "Concatenated String ${str}"
Note that the function
keyword is redundant with ()
, but function
is less portable.
请注意,function
关键字与 是多余的()
,但function
可移植性较差。
回答by mr.spuratic
A more flexible, but slightly harder to understand approach is to pass a variable name, and use eval
so that the variable becomes set in the caller's context (either a global or a function local). In bash:
一种更灵活但稍微难以理解的方法是传递一个变量名,并使用它eval
以便在调用者的上下文中设置变量(全局或局部函数)。在 bash 中:
function mylist()
{
local _varname= _p _t
shift
for _p in "$@"; do
_t=$_t[$_p]
done
eval "$_varname=$_t"
}
mylist tmpvar a b c
echo "result: $tmpvar"
On my Linux desktop (bash-3.2) it's approx 3-5x faster (10,000 iterations) than using ``
, since the latter has process creation overheads.
在我的 Linux 桌面 (bash-3.2) 上,它比使用 快约 3-5 倍(10,000 次迭代)``
,因为后者具有进程创建开销。
If you have bash-4.2, its declare -g
allows a function to set a global variable, so you can replace the unpretty eval
with:
如果你有 bash-4.2,它declare -g
允许一个函数设置一个全局变量,所以你可以eval
用:
declare -g $_varname="$_t"
The eval
method is similar to TCL'supvar 1
, and declare -g
is similar to upvar #0
.
该eval
方法类似于TCL的upvar 1
,和declare -g
类似upvar #0
。
Some shell builtins support something similar, like bash's printf
with "-v", again saving process creation by assigning directly to a variable instead of capturing output (~20-25x faster for me).
一些 shell 内置函数支持类似的东西,比如printf
带有“-v”的bash ,再次通过直接分配给变量而不是捕获输出来保存进程创建(对我来说快 20-25 倍)。