-
Notifications
You must be signed in to change notification settings - Fork 7.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Equivalent of bash set -e
#3415
Comments
|
As someone fresh to Powershell that's a confusing distinction. So would $erroractionpreference = "stop" work for my example above..? i.e.
but
|
|
Would probably want to throw the exception only in a context where Powershell also sets $? to false as a result. Throwing an exception in other situations where lastexitcode != 0 would not give a result equivalent to "set -e" in bash. https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html |
Hi, i've done some work on this, can I ask for an assignment? |
Add preference to throw exception on $lastexitcode!=0, like bash ’set -e’. Only throw with commands *not* used in an expression/if/loop value. $NativeCommandPipeFailPreference=“continue”: like ‘set -e;set +o pipefail’. An exception is thrown for a native command giving a non-zero exit code, if it is the the last command in the pipeline. $NativeCommandPipeFailPreference=“silentlycontinue”: like ‘set +e’. No exception is thrown on non-zero exit codes. This is the default. $NativeCommandPipeFailPreference=“stop”: like ‘set -e;set -o pipefail’. An exception is thrown for a native command giving a non-zero exit code. With $NativeCommandExceptionPreference "continue" (the default), a RuntimeException is thrown, handing off to PowerShell error handling. With $NativeCommandExceptionPreference “stop”, an ExitException is thrown, terminating the shell regardless of other PowerShell error handling.
@mopadden consider it assigned, GitHub doesn't allow assigning to people without write access |
Assigning to @daxian-dbw who's assigned to the PR so no one else duplicates this work |
Anyone who's interested in this should review the RFC: https://github.com/PowerShell/PowerShell-RFC/pull/88/files |
+1, very inconvenient to check $LastExitCode, especially when using PS for build scripts with multiple steps... |
Since this is the first search result, linking the most recent thread on this RFC that I can find for anyone interested in implementing it PowerShell/PowerShell-RFC#277 |
The PR that implements the RFC was already merged as an experimental feature: #15897 |
Is the equivalent of Enable-ExperimentalFeature PSNativeCommandErrorActionPreference Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$PSNativeCommandUseErrorActionPreference = $true |
I asked this question on twitter and it was suggested I post here.
Many people consider it good practice to start a bash script.
set -e
tells the script to stop on first errorset -u
tells the script to not allow undeclared variablesAlthough not everyone agrees
I think
set -u
andSet-StrictMode -Version 1.0
are equivalent.I just wrote a set of Powershell scripts for managing an elasticsearch server. Then a few top-level scripts to tie them together.
It would have been really useful to be able to write something equivalent to...
... but I couldn't find how to and ended up with something more like...
Apologies if this is already possible and I've missed it...
The text was updated successfully, but these errors were encountered: