简体   繁体   中英

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? The return code (eg, exit code, return status, exit status, returnCode) is from a SQL*Plus command if that matters.

kornShellSnippet1.ksh

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

kornShellSnippet2.ksh

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

-ne is a numeric test, != is a string test. 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.

For comparing numbers though, wouldn't you use the arithmetic syntax?

let returnCode=${?}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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