0

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.

  • 1
    Possible duplicate of [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) – double-beep Feb 01 '19 at 12:40
  • Try with `call echo %^ErrorLevel%`, or alternatively with `cmd /V /C` and `echo !ErrorLevel!`. Also try to remove `start`, or to use `start "" /WAIT` instead. – aschipfl Feb 01 '19 at 13:13
  • Have you tried my answer? – double-beep Feb 01 '19 at 16:10
  • I've tried these, I can't add double quotes in the line, and the other suggestions didn't help. I guess the real questions here are : 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 ? – randomLainist Feb 01 '19 at 16:15
  • @randomLainist `"shell_cmd": "gcc -Wall -pedantic $file_name -o ${file_base_name} && cmd /v /k "${file_path}/${file_base_name} & echo !errorlevel!""` – double-beep Feb 01 '19 at 16:25
  • @double-beep it won't accept this syntax – randomLainist Feb 01 '19 at 16:33
  • Can you make it `"shell_cmd": "gcc -Wall -pedantic $file_name -o ${file_base_name} && start filename.bat`? `filename.bat` will contain `@echo off & setlocal EnableDelayedExpansion & start path\to\file\file.exe & echo !errorlevel!`. Optionally expand it. – double-beep Feb 01 '19 at 16:44
  • @double-beep it kills the interest of having a build configuration, I would have to create a new filename.bat for each new project. The goal here is to automatically build then run my code, which works with my actual build config, but I would like to automatically display the exit code after running my code. I manually tried to evaluate `echo %errorlevel%` into cmd after running the code and the value displayed is correct btw. I just need a way to echo automatically after running into the external cmd – randomLainist Feb 01 '19 at 17:12
  • I cannot help in this way, then. Do not know anything about C. You need to start cmd with `/v` and replace `%errorlevel%` with `!errorlevel!`. Why isn't this exactly not working? Can you post a link of a screenshot here? – double-beep Feb 01 '19 at 17:14
  • I will edit my op with a new screenshot and more precisions. I'm sorry, I know I'm not making so much sense, English is not my native language and I'm only a student trying to understand things out of my range. – randomLainist Feb 01 '19 at 17:17

2 Answers2

0

You will need to use delayed expansion to achieve this. errorlevel is set before you run your command and it want change in the same line. Use cmd /v to enable it for cmd:

"shell_cmd": "gcc -Wall -pedantic $file_name -o ${file_base_name} && start cmd /v /k ${file_path}/${file_base_name} & echo !errorlevel!  "
double-beep
  • 4,567
  • 13
  • 30
  • 40
  • With this code `echo !errorleve!` is evaluated in sublimetext's console and displays !errorlevel! – randomLainist Feb 01 '19 at 16:10
  • `"shell_cmd"` is the target of this command, it's basically cmd. The thing is that sublimetext doesn't support inputs so I need to run my exe into an external cmd. Hence the `&& start cmd`. The problem is that I don't know how to transmit two commands to this new cmd. – randomLainist Feb 01 '19 at 16:31
0

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.

double-beep
  • 4,567
  • 13
  • 30
  • 40