Reputation: 517
Someone wanted me to convert his bash code into powershell. I thought I could do it until I came across a weird built in variable called $NF
which I think reads the lines of the logger? I am not sure how it works but because of it, it stops me from converting this bash script into powershell.
The specific two lines I am having a hard time with: bash script
curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null
curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" > ${filename}
but I am not sure what to write for $NF
in powershell. The other variables, I know how to convert, just not this built in $NF variable to download the item from google drive.
Upvotes: 1
Views: 328
Reputation: 59
I think more to the point of the general question about awk's magic variable NF -- number of fields on the line -- and its use in $NF -- the field at the rightmost position on the line is to note that Powershell's arrays can reference the final position using the index -1. (Perhaps easily remembered with: indexes 1, 2, 3... are from the left; indexes -1, -2, -3 are from the right.)
So as an example this morning I used:
ipconfig | Where-Object {$_ -match 'IPv4 Address'} | %{($_ -split "\s+")[-1]}
to extract the IP addresses from the rightmost positions.
(I haven't worked out the specific curl example above -- in powershell I usually use Invoke-WebRequest in place of curl, but I think I've answered the $NF question and solving the specific question could proceed from there.)
Upvotes: 1
Reputation: 8377
As @Cyrus commented, $NF belongs to awk, not Bash.
Good news though: it should make working thru the equivalent really easy, since you can just run awk from PowerShell.
Since you can also call curl directly, I'll just modify the example you have.
# Assigning this to a variable for readability
$curlArg = "https://drive.google.com/uc?export=download&confirm=$(
<#
This is a PowerShell subexpression.
the output of this subexpression will be included in the string
(multiple outputs will be separated by ' ' )
Executables may return multiple lines of output (aka multiple outputs), but this case looks like it will only return one line.
If you need to combine the .exe's output, try -join.
#>
awk '/download/ {print $NF}./cookie'
)&id=$fileid"
curl -Lb ./cookie $curlArg > $filename
Hope this Helps
Upvotes: 2