I want to determine on macOS which version of .NET runtimes I have installed.
I'm using command dotnet --list-runtimes
to print available versions.
Microsoft.AspNetCore.App 6.0.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.11 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.11 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Would love to create an array with above versions like ["6.0.9, "6.0.11"]
to be able to see if there's a version higher or equal than, for example, 6.0.11.
I have a code that looks like this:
if [[ -f "/usr/local/share/dotnet/dotnet" ]]
then
IFS=' '
declare sdks=$(dotnet --list-runtimes)
for runtime in "${sdks}"
do
echo $runtime
declare split=("")
read -a split <<< $runtime
echo ${split[1]}
done
IFS=''
else
echo "ERROR: Unable do determine installet .NET SDK."
fi
Unfortunately echo ${split[1]}
prints only once 6.0.9.
CodePudding user response:
Check:
$ array=( $(dotnet --list-runtimes | awk '{a[$2]=""}END{for (i in a) print i}') )
$ printf '%s\n' "${array[@]}"
6.0.9
6.0.11
CodePudding user response:
If you want to print unique versions then use:
dotnet --list-runtimes | awk '!seen[$2] {print $2}'
If you want to create an array in bash 3.2 then use:
vers=()
while read -r v; do
vers =("$v")
done < <(dotnet --list-runtimes | awk '!seen[$2] {print $2}')
# check array content
declare -p vers
declare -a vers=([0]="6.0.9" [1]="6.0.11")
Breakdown:
awk '!seen[$2] {print $2}'
: prints unique version numbers from field #2< <(...)
is process substitutionreadarray
populates given arrayvers
using output of process substitution
CodePudding user response:
Here's a bash version:
#!/usr/bin/env bash
declare -a sdks
while read -r runtime; do
set $runtime
sdks =("$2")
done < <(dotnet --list-runtimes)
echo "${sdks[@]}"
CodePudding user response:
Does this work for your purposes?
dotnet --list-runtimes | cut -d ' ' -f 2 | sort | uniq | xargs
6.0.11 6.0.9
Update
As used in a loop:
> for version in $(dotnet --list-runtimes | cut -d ' ' -f 2 | sort | uniq | xargs); do echo $version; done
6.0.11
6.0.9