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



以下两个 KornShell (ksh) 代码段之间有什么区别,因为它们在测试期间到目前为止的行为完全相同?返回代码(例如,退出代码、返回状态、退出状态、返回代码)来自 SQL*Plus 命令(如果这很重要)。

kornShellSnippet1.ksh

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

kornShellSnippet2.ksh

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

-ne是一个数字测试,!=是一个字符串测试。由于您知道$?是一个数字,因此使用数字测试是有意义的。

我所知-ne遗留语法支持向后兼容,而(至少在ksh双方括号内)!=等是本机ksh语法。

不过,为了比较数字,您不会使用算术语法吗?

let returnCode=${?}
if (( returnCode != 0 )); then #successful command returns 0#

最新更新