Hacker News new | ask | show | jobs
by nizmow 2346 days ago
First few lines of the link answers that question:

cmd1; if ($?) { cmd2 } = cmd1 && cmd2

cmd1; if (-not $?) { cmd2 } = cmd1 || cmd2

Just your usual Powershell verboseness.

2 comments

The problem is that many old utilities in Windows don't use the exit code to indicate failure. PowerShell cmdlets have exceptions that can be caught with `try`/`catch`, and there's `-ErrorAction Stop`, but there was no good solution for native commands.
Their logical operators page says it also supports -and and -or. I can't tell if "cmd1 -and cmd2" would have already worked. It sounds like unadorned "cmd1" didn't return the equivalent of $? before this PR.
No it wouldn’t work, cmd1 would return the stdout output of cmd1 if you could do that at all, but it would really try to use -and cmd2 as parameters to cmd1.
Interesting. Switching back and forth from Powershell and bash must be hard. Also $? has the exact opposite meaning in a Unix shell. $? as zero indicates success.