How do I get the application exit code from a Windows command line?

后端 未结 7 764
执笔经年
执笔经年 2020-11-22 02:13

I am running a program and want to see what its return code is (since it returns different codes based on different errors).

I know in Bash I can do this by running<

7条回答
  •  自闭症患者
    2020-11-22 02:44

    At one point I needed to accurately push log events from Cygwin to the Windows Event log. I wanted the messages in WEVL to be custom, have the correct exit code, details, priorities, message, etc. So I created a little Bash script to take care of this. Here it is on GitHub, logit.sh.

    Some excerpts:

    usage: logit.sh [-h] [-p] [-i=n] [-s] 
    example: logit.sh -p error -i 501 -s myscript.sh "failed to run the mount command"
    

    Here is the temporary file contents part:

    LGT_TEMP_FILE="$(mktemp --suffix .cmd)"
    cat<$LGT_TEMP_FILE
        @echo off
        set LGT_EXITCODE="$LGT_ID"
        exit /b %LGT_ID%
    EOF
    unix2dos "$LGT_TEMP_FILE"
    

    Here is a function to to create events in WEVL:

    __create_event () {
        local cmd="eventcreate /ID $LGT_ID /L Application /SO $LGT_SOURCE /T $LGT_PRIORITY /D "
        if [[ "$1" == *';'* ]]; then
            local IFS=';'
            for i in "$1"; do
                $cmd "$i" &>/dev/null
            done
        else
            $cmd "$LGT_DESC" &>/dev/null
        fi
    }
    

    Executing the batch script and calling on __create_event:

    cmd /c "$(cygpath -wa "$LGT_TEMP_FILE")"
    __create_event
    

提交回复
热议问题