question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Script via STDIN does not exit with error code and does not stop correctly

See original GitHub issue

Steps to reproduce

Link to a related Problem in GitLab: https://gitlab.com/gitlab-org/gitlab-runner/-/issues/27830#note_564599211

Test.ps1:

$ErrorActionPreference = 'Stop'
just bogus
Write-Host 'hello'

Batch-Call (working correctly with file)

pwsh -File Test.ps1

Exit-Code: 1

Batch-Call (working correctly with stdin)

type Test.ps1 | pwsh -noprofile -noninteractive -command "$input | iex"

Exit-Code: 1

Batch-Call (not working correctly with stdin)

type Test.ps1 | pwsh -Command -

Exit-Code: 0

Expected behavior

Powershell quits with non zero exit code and does not print "hello", but an exception for "just bogus"

Actual behavior

Powershell does execute all statement even though $ErrorActionPreference is set to "stop"

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:28 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
Scordocommented, May 2, 2021

Found out, that with 2 linebreaks behind the command it is working:

& {
    $ErrorActionPreference = 'Stop'
    just bogus
    Write-Host 'hello'
}


Just 2 empty lines behind the closing bracket and it is working.

The other working one is:

$pwshcode = {
    $ErrorActionPreference = 'Stop'
    just bogus
    Write-Host 'hello'
}

& $pwshcode
1reaction
jborean93commented, Jan 19, 2022

I wouldn’t say it’s completely unusable there are workarounds that make this entirely possible. The method I usually use is this

docker run \
  --rm \
  --interactive \
  mcr.microsoft.com/powershell pwsh -Command - << 'EOF'
& {
    if ($true) {
        echo "hi"
    }
}

EOF

Basically make sure there is an empty line between `}` and `EOF` so the input is accepted by pwsh and also put all your code inside a `&{}` block to ensure it is run as 1 script rather than the line by line parser that is the standard console input code.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does the script command not return the correct exit ...
Simply put: $ script -V script from util-linux 2.23.2 $ cat /etc/centos-release CentOS Linux release 7.9.2009 (Core) $ false && echo This is...
Read more >
Make bash script exit if no user input in stdin
The issue is that if you just press enter the script executes as though a "y" or a "Y" have been input, and...
Read more >
set -e subshell doesn't exit on the first command that fails if ...
I have a script.sh and I have to correctly diagnose any errors inside it, so the prova function in case of an error...
Read more >
How do I close stdin in a shell script? - bash
I have a misbehaving program that I need to call from my script. It exits as soon as it sees something on stdin....
Read more >
Thread: psql dones't reflect exit status if input command via stdin
Hello, I found when running command like `# echo "xxx" | psql postgres postgres`, the return code is always 0 even though the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found