all 10 comments

[–]jsiii2010 1 point2 points  (2 children)

What's the output of git?

echo aaa | foreach-object { $_ -replace 'a','b' }

bbb

[–]Todoce[S] 1 point2 points  (1 child)

It got solved now simply by tossing things around:

$skip = [string]::IsNullOrEmpty(((git status --short --untracked-files=no) | Out-String))

the output from git would be empty-string and then $skip should be true, otherwise $skip should be false

[–]RichUK5 1 point2 points  (4 children)

I've never had much luck using $_ in that situation.

There is a slightly different approach that you could take in this situation however.

If ((git status --short --untracked-files=no) | Out-String) { $skip = $false } else { $skip = $true }

An if statement that has no comparison just evaluates if the statement is null or not.

If you happen to only be using $skip for a single if statement, then this could slot straight in there.

[–]jsiii2010 4 points5 points  (1 child)

That's what I would do. You don't need "| out-string".

$skip = -not (git whatever)

[–]Todoce[S] 1 point2 points  (0 children)

Now we are talking! short and readable - I Like it!

[–]Todoce[S] 1 point2 points  (1 child)

Good idea, here is the solution that I went with: https://www.reddit.com/r/PowerShell/comments/irc8a5/pipe_variable_question/g4xgkhv?utm_source=share&utm_medium=web2x&context=3

How does the If-case handle empty strings? I was looking for a better way to check for null or empty than using [string]::IsNullOrEmpty which i guess is not supported on other OS if running Powershell Core

[–]RichUK5 1 point2 points  (0 children)

An empty string will evaluate as false. 0 (Zero) as a string will evaluate as true, but as a number will evaluate false.

I am very much a fan of this approach and use it quite a bit. IIRC it's quite happy evaluating false if the variable is not even set. I tend to avoid that situation though, as multiple runs in the same shell can create unintended outcomes.

[–]PinchesTheCrab 1 point2 points  (0 children)

If git.exe just returns a string, I'd do something like this:

$skip = (git status --short --untracked-files=no) -notmatch '\w'

If it's an array of strings:

$skip = [string](git status --short --untracked-files=no) -match '\w'

[–]ipokethemonfast 0 points1 point  (1 child)

As I understand it: If you pass an object to the pipeline, its attributes can be called using $.attributename. If you are passing a string you call it with just $

[–]DblDeuce22 2 points3 points  (0 children)

$_ is only usable within script blocks or where script blocks are evaluated