Linux 括号中的 ksh 函数返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8873431/
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
ksh function return value in parentheses
提问by
In the following very simple ksh script example, I need to ask if func1 results equal to 4 ,
在以下非常简单的 ksh 脚本示例中,我需要询问 func1 结果是否等于 4 ,
This is what I did in the example but this script does not print the "function result = 4" as I expected it to.
这就是我在示例中所做的,但该脚本没有像我预期的那样打印“函数结果 = 4”。
What do I need to change in the [[......]] in order to print the "function result = 4"
我需要在 [[......]] 中更改什么才能打印“函数结果 = 4”
Remark - func1 must be in the [[.....]]
备注 - func1 必须在 [[.....]]
#!/bin/ksh
func1()
{
return 4
}
[[ ` func1 ` = ` echo $? ` ]] && print "function result = 4"
采纳答案by shellter
You need
你需要
#!/bin/ksh
func1()
{
print -- 4
}
[[ $(func1) = 4 ]] && print "function result = 4"
OR
或者
#!/bin/ksh
func1()
{
return 4
}
func1 ; [[ $? == 4 ]] && print "function result = 4"
There are several issues in the code that you present, so let me try to explain (You're making it more complicated than it need be).
您提供的代码中有几个问题,所以让我尝试解释一下(您使它变得比需要的更复杂)。
No. 1 is your use of back-ticks for command substitution, these have been deprecated in the ksh language since ~ 1995! Use $( ... cmd ) for modern cmd-substitution. We often see backticks listed as a nod to portability, but only scripts written for systems where the Bourne shell is the onlyshell available require the use of backticks. (well, I don't know about dash or ash, so maybe those too).
第一是您使用反引号进行命令替换,自 1995 年以来,这些在 ksh 语言中已被弃用!使用 $( ... cmd ) 进行现代 cmd 替换。我们经常看到反引号被列为对可移植性的认可,但只有为 Bourne shell 是唯一可用shell 的系统编写的脚本需要使用反引号。(好吧,我不知道破折号或灰烬,所以也许也是如此)。
No 2. is that $? gets set after ever function or command or pipeline is executed and is the return code of that last command. It is a value between 0-255. When you have code like cmd ; rc=$? ; echo $?
; you're now echoing the status of the assignment of rc=$?
(which will almost always be 0), AND that is why you will see experienced scriptors save the value of $? before doing anything else with it.
不 2. 那是 $ 吗?在函数或命令或管道执行后设置,并且是最后一个命令的返回码。它是一个介于 0-255 之间的值。当你有这样的代码时cmd ; rc=$? ; echo $?
;您现在正在回显rc=$?
(几乎总是 0)的分配状态,这就是为什么您会看到有经验的脚本编写者保存 $? 在用它做任何其他事情之前。
Recall that command-substitution uses what ever is the output of the $( ... cmd ...)
or backtics enclosed command while return
sets the value of $?
(until the very next command executionresets that value).
回想一下,命令替换$( ... cmd ...)
在return
设置值时使用或 backtics 封闭命令的输出$?
(直到下一个命令执行重置该值)。
I hope this helps.
我希望这有帮助。
回答by kubanczyk
Function does return 4. The operator `` (backticks) ignores the result value, and returns the function's stdout instead (in your case an empty string, since func1 did not print anything to stdout).
函数确实返回 4。运算符 ``(反引号)忽略结果值,而是返回函数的标准输出(在您的情况下为空字符串,因为 func1 没有向标准输出打印任何内容)。
And
和
`echo $?`
is just over-complicated way of saying
只是过于复杂的说法
$?