I asked this question on twitter and it was suggested I post here.
Many people consider it good practice to start a bash script.
#! /bin/bash
set -eu
set -e
tells the script to stop on first error
set -u
tells the script to not allow undeclared variables
Although not everyone agrees
I think set -u
and Set-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...
set -e
$someValue = .\server-task-1.ps1
.\server-task-2.ps1 -using $someValue
.\task-that-requires-everything-else-suceeded.ps1
... but I couldn't find how to and ended up with something more like...
$someValue = .\server-task-1.ps1
if ($LastExitCode -ne 0) {
exit $LastExitCode
}
.\server-task-2.ps1 -using $someValue
if ($LastExitCode -ne 0) {
exit $LastExitCode
}
.\task-that-requires-everything-else-suceeded.ps1
if ($LastExitCode -ne 0) {
exit $LastExitCode
}
Apologies if this is already possible and I've missed it...
$erroractionpreference = "stop"
is similar to set -e
, except it only works against cmdlets and not native commands
As someone fresh to Powershell that's a confusing distinction.
So would $erroractionpreference = "stop" work for my example above..?
i.e.
$erroractionpreference = "stop"
$someValue = .\server-task-1.ps1
# does not run the next two commands if server-task-1.ps1 exits with code != 0
.\server-task-2.ps1 -using $someValue
.\task-that-requires-everything-else-suceeded.ps1
but
$erroractionpreference = "stop"
$someValue = .\server-task-1.exe
# does run the next two commands _even though_ server-task-1.exe exits with code != 0
.\server-task-2.ps1 -using $someValue
.\task-that-requires-everything-else-suceeded.ps1
$erroractionpreference = "stop"
only works if Write-Error
is called (such as an exception is raised). It doesn't do any checks against $lastexitcode. We can certainly look into adding another preference variable to throw an exception if $lastexitcode != 0.
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
Edit: the idea is to throw an error on failure where the success/failure of an application is not tested.
Hi, i've done some work on this, can I ask for an assignment?
@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...
Most helpful comment
$erroractionpreference = "stop"
only works ifWrite-Error
is called (such as an exception is raised). It doesn't do any checks against $lastexitcode. We can certainly look into adding another preference variable to throw an exception if $lastexitcode != 0.