Skip to content

Equivalent of bash set -e #3415

@pauldambra

Description

@pauldambra

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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions