How do I negate the exit code of an executable in powershell? I would like my script to succeed if an executable terminated with a non-zero exit code, or fail if the executable terminated with a zero exit code.
I tried the following ! (...) and it prints True, which sounds promising, however it also makes the script fail with exit code 1.
! (program.exe) # I want this line to fail if program.exe returns 0
Output:
True
Error: Process completed with exit code 1.
Note that I'm just interested in checking the exit code of the executable. If the executable fails for other reasons (e.g. missing libraries) then it's fine for the script to fail.
This question has some similarities to this question for -nix systems. However, I'm writing a powershell script to be run on Windows 10.
Related
When I start a new bash shell, if I run the command echo $? as the first thing, I get 1. How can I run bash with the "default" exit code being 0?
Context: I am running msys2 in a terminal window in VS Code. If I start msys2, and then realize I didn't need a shell now and just type exit, bash exits with 1, causing VS Code to pop up an annoying warning.
Most likely something in your profile is failing and setting the status code to 1. Since status codes are overwritten by each process that runs, it'll probably be something towards the end.
I have a bash script that runs a list of small programs mostly written in C and Python, since the programs themselves are NOT bug free and they might crash or run into infinite loop, so in the BASH script, I run the programs in subshell so it won't break the main shell, here is what it likes:
#!/usr/bin/env bash
set -e
for py_p in "${py_program_list[#]}"; do
(python "$py_p") || echo "terminated!"
done
for c_p in "${c_program_list[#]}"; do
("$c_p") || echo "terminated!"
done
The problem is, when loops in python programs, the bash script won't be affected by any error in the python program which is what I expected. However, the bash script exit immediately if any C program exit with error.
UPDATE:
I am using BASH 3.2 in OSX 10.9.5
UPDATE 2:
Updated the question to make it more clear, sorry for the confusion. The problem I have is about the C program, the python part confirm the error in subshell won't affect the main shell but the C program breaks the rule.
the Python scripts are fine, no matter I use Ctrl + C or they crash
for some reason, they won't stop the main shell from running which is
what I expect. But the C programs don't, type Ctrl + C when a C
program is running will exit the bash script.
Python handles the interrupt signal itself (outputting Traceback …KeyboardInterrupt) and then terminates normally, returning the exit status 1 to bash.
Your C programs evidently don't handle the signal, so the default action is taken, to terminate the process; bash is informed that the program was terminated by signal SIGINT.
Now bash behaves differently depending on the kind of the child program's termination (normal or signaled): In the first case, it continues execution with || echo "terminated!", in the second case, it terminates itself, as you observed.
You can change that behavior by trapping the signal in your script, e. g. by inserting
trap "echo interrupted" INT
somewhere before the for c_p loop.
Everything depends on the Python programs exit status. Maybe they return the same value regardless they fact their execution was successful or not. So... basically, you cannot rely on their exit status.
I'm trying to write a script in Fish that runs a Make recipe and then executes all of the resultant binaries. The problem I'm having is that I would like to have the script exit with an error code if the make command encounters an error. Whenever I try to capture Make's return value, I end up with its output log instead.
For example:
if test (make allUnitTests) -eq 0
echo "success"
else
echo "fail"
end
returns an error because "test" is seeing the build process, not the terminating condition.
I wrote the script so that I could easily make Jenkins run all my unit tests whenever I trigger a build. Since I haven't been able to get the above section of the script working correctly, I've instead instructed Jenkins to run the make command as a separate command, which does exactly what I want: halting the entire build process without executing any binaries if anything fails to compile. Thus, at this point my question is more of an academic exercise, but I would like to add building the unit test binaries into the script (and have it cleanly terminate on a build error) for the benefit of any humans who might check out the code and would like to run the unit tests.
I played a little with something like:
if test (count (make allUnitTests | grep "Stop")) -eq 0
but this has two problems:
I'm apparently piping stdout when I need to pipe stderr. (Come to think of it, if I could just check to see if anything was written to stderr, then I wouldn't need grep at all.)
Grep is swallowing all the log data piped to it, which I really want to be visible on the console.
You are misunderstanding the parentheses - these run a command substitution. What this does is capture the output of the process running in the substitution, which it will then use as arguments (separated by newlines by default) to the process outside.
This means your test will receive the full output of make.
What you instead want to do is just run if make allUnitTests without any parens, since you are just interested in the return value.
If you would like to do something between running make and checking its return value, the "$status" variable always contains the return value of the last command, so you can save that:
make allUnitTests
set -l makestatus $status
# Do something else
if test $makestatus -eq 0
# Do the if-thing
else
# Do the else-thing
end
I have a program x which sometimes crashes on certain input files.
How do I write a bash script that returns the following?
0 if the program x terminates fine or runs for longer than 1/20th of a second
1 if the program x segfaults
Note that the program will segfault or run forever, so I need to stop it somehow with the script. can you show me please
Thank you for any ideas
Most of the programs when they do not terminate correctly return 0. That information can be gleaned from the bash variable $?. So, after you run the program, check if $? is 0. If it is, the program ran successfully. Otherwise, there was a problem.
This is, of course, assuming that the program is following proper conventions.
echo $? should let you know whether or not the program succeeded.
http://www.devshed.com/c/a/BrainDump/Executing-Commands-with-bash/1/
I compile an NSIS script to a .exe install file. I launch the .exe with command line \S silent option.
Installation performs silently as wanted, but there is exit code 1. Exit code 1 corresponds to case with user choosing cancel on the wizard. However, install is successful and mode is silent (no user interaction). Also, where does this exit code comes from, and how to manually enforce an exit code 0?
I have an idea i could do something in .onInstSuccess function, to enforce an exit code 1 if installation is successful.
Also, ExecWait is capturing the exit code into a variable, but has got no 'set' option.
What would you recommend?
Thanks and regards
Without any sample code it is a bit hard to guess what the problem could be!
You can set a specific exit code with SetErrorLevel.
As far as ExecWait goes, setting anything makes no sense, when it returns the child process has ended. If you want to use the exit code of a child process all you need is to get it:
ExecWait '"c:\foo.exe"' $0
SetErrorLevel $0