Search code examples
cmdsublimetext3exit-code

Sublimetext 3 : Unable to get exit code in external cmd


I am running a C code from sublimetext 3 with GCC on Windows 10 and I would like the external cmd to show exit code after running the exe.

Here is my sublime-build file:

{
 "shell_cmd" : "gcc -Wall -pedantic $file_name -o ${file_base_name}",
 "working_dir" : "$file_path",
 "variants":
  [
   {
     "name": "Run",
     "shell_cmd": "gcc -Wall -pedantic $file_name -o ${file_base_name} && start cmd /k ${file_path}/${file_base_name}  "
   }
  ]
}

I've tried something like this:

"shell_cmd": "gcc -Wall -pedantic $file_name -o ${file_base_name} && start cmd /k ${file_path}/${file_base_name} & echo %errorlevel%  "

The problem with this one is that the exit code is showing up in the sublimetext shell, always displaying 0. I guess it's the external cmd exit code.

I would like the echo %errorlevel% part to be executed in the new cmd and to display my exe's exit code but I can't figure out how to do it.

EDIT : Here is a screenshot :

running hello world

My main.exe runs in a new cmd and the echo is done back in sublimetext's own console.

I guess these questions sum it up in a better way :
1- How do I write this script so that cmd can execute a second command ?
2- How do I make it open, run my .exe, and only then display the exit code ?

EDIT 2 :

Here is a new screenshot.

Here you can see my code and the external cmd called by sublimetext's internal cmd with the command from my sublime-build file.
I manually entered echo %errorlevel%into this external cmd.
The goal I'm trying to achieve is to modify the command from my sublime-build file, which will be executed from sublimetext's internal cmd, in such a way that the external cmd called runs my code and only then evaluate echo %errorlevel%, all of this automatically.


Solution

  • I found the solution after some additional googling.

    I needed to escape the &using ^&

    Here is my new sublime-build command:

    "shell_cmd": "gcc -Wall -pedantic $file_name -o ${file_base_name} && start cmd /V /k ${file_path}/${file_base_name} ^& echo: ^& echo: ^& echo: ^& echo program ended code : !errorlevel! "
    

    And a screenshot of the result.