简体   繁体   English

使用“ -ne”和“!=”来评估KornShell(ksh)中的退出代码有什么区别?

[英]What is the difference between using “-ne” and “!=” for evaluating the exit code in KornShell (ksh)?

What is the difference between the following two KornShell (ksh) snippets because they both behave exactly the same thus far during testing? 以下两个KornShell(ksh)代码段之间的区别是什么,因为到目前为止它们在测试期间的行为都完全相同? The return code (eg, exit code, return status, exit status, returnCode) is from a SQL*Plus command if that matters. 如果重要,则返回代码(例如,退出代码,返回状态,退出状态,returnCode)来自SQL * Plus命令。

kornShellSnippet1.ksh kornShellSnippet1.ksh

returnCode=${?}
    if [[ ${returnCode} -ne 0 ]]; then #successful command returns 0#

kornShellSnippet2.ksh kornShellSnippet2.ksh

returnCode=${?}
    if [[ ${returnCode} != 0 ]]; then #successful command returns 0#

-ne is a numeric test, != is a string test. -ne是数字测试, !=是字符串测试。 Since you know $? 既然您知道$? is a number, it makes sense to use the numeric test. 是一个数字,使用数字测试很有意义。

As far as I know -ne is legacy syntax supported for backward compatibility, while (within ksh double square brackets at least) != etc are the native ksh syntax. 据我所知, -ne是支持向后兼容的传统语法,而(至少在ksh双方括号内) !=等是本机ksh语法。

For comparing numbers though, wouldn't you use the arithmetic syntax? 为了比较数字,您不使用算术语法吗?

let returnCode=${?}

if (( returnCode != 0 )); then #successful command returns 0#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM