UNIX:可以绕过unix的退出代码吗?
我正在编写一些 Shell 代码,对于某些编程逻辑,我需要使用负数进行一些返回。这是:
if condition ; then
return -1
else
return -2
fi
尽管如此,使用负数时我会遇到错误,可能是因为: Unix 退出状态仅限于值 0-255,即无符号 8 位整数的范围。
(来自 http://en.wikipedia.org/wiki/Exit_status#Unix)
有没有办法绕过这个? (我知道我可以使用其他返回号码)
谢谢。
I´m writing some Shell code and, for some logic of programming, I need to do some returns with negative numbers. This is:
if condition ; then
return -1
else
return -2
fi
Nevertheless, I get errors when using negative numbers, maybe because: Unix exit statuses are restricted to values 0-255, the range of an unsigned 8-bit integer.
(from http://en.wikipedia.org/wiki/Exit_status#Unix)
Is there a way to bypass this? (I know that I could use another return numbers)
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,shell 脚本的 Unix 标准是 exit 0 表示成功,退出非零表示不成功。
您能做的最好的事情就是捕获返回值并根据需要使用它们,即
不是您的霸主想听到的,而是将它们参考 Posix 标准。
另外仅供参考,
$()
称为命令替换。您将看到人们还使用成对的反引号“cmd
”实现命令子,但使用$( cmd )
,除非您在 Sun/AIX 上使用原始的 shell 编码或其他传统供应商平台,或者您需要创建完全向后兼容的代码(重点是向后!)。$()
很好,因为您可以根据需要嵌套它们,即根据新的 Kornshell 编程语言(ISBN-10:0131827006,1995!)反引号已被弃用。
请注意,任一类型的命令替换都会创建一个子 shell 来运行命令,然后将结果“替换”到命令行中。
我希望这有帮助;-)
Sorry, but the Unix standard for shell scripting is exit 0 for success and exit non-zero for non-success.
The best you can do is capture return values and use them as you want, i.e.
Not what your overlords want to hear, but refer them to the Posix Standards.
Also FYI,
$()
is called command substitution. You will see people also implement command sub with paired back ticks 'cmd
', but use the$( cmd )
, unless you are using original borne shell coding on Sun/AIX or other heritage vendor platforms OR you are required to create code that is completely backwards (with the emphasis on backwards!) compatible.$()
is nice because you can nest them as much as you need, i.e.According the New Kornshell Programming Language (ISBN-10: 0131827006, 1995!) backticks are deprecated.
Note that either type of command substitution is creating a sub-shell to run the command, and then 'substitute in' the results into your command line.
I hope this helps ;-)