rtrouton / rtrouton_scripts

Scripts to share
MIT License
1.3k stars 489 forks source link

Enhancement request: Improvement if softwareupdate -l lists multiple results #96

Closed jelockwood closed 1 year ago

jelockwood commented 1 year ago

The script says -

    # Check to see if the softwareupdate tool has returned more than one Xcode
    # command line tool installation option. If it has, use the last one listed
    # as that should be the latest Xcode command line tool installer.

However when I checked the result it actually returned the following which as you can see does not list the newest version as the last line.

Command Line Tools for Xcode-12.4
Command Line Tools for Xcode-13.2
Command Line Tools for Xcode-12.5
Command Line Tools for Xcode-12.5

This can be easily solved by adding | sort to the end of the relevant lines as follows

    if [[ ( ${osvers_major} -eq 10 && ${osvers_minor} -ge 15 ) || ( ${osvers_major} -ge 11 && ${osvers_minor} -ge 0 ) ]]; then
       cmd_line_tools=$(softwareupdate -l | awk '/\*\ Label: Command Line Tools/ { $1=$1;print }' | sed 's/^[[ \t]]*//;s/[[ \t]]*$//;s/*//' | cut -c 9- | sort) 
    elif [[ ( ${osvers_major} -eq 10 && ${osvers_minor} -gt 9 ) ]] && [[ ( ${osvers_major} -eq 10 && ${osvers_minor} -lt 15 ) ]]; then
       cmd_line_tools=$(softwareupdate -l | awk '/\*\ Command Line Tools/ { $1=$1;print }' | grep "$osvers_minor" | sed 's/^[[ \t]]*//;s/[[ \t]]*$//;s/*//' | cut -c 2- | sort)
    elif [[ ( ${osvers_major} -eq 10 && ${osvers_minor} -eq 9 ) ]]; then
       cmd_line_tools=$(softwareupdate -l | awk '/\*\ Command Line Tools/ { $1=$1;print }' | grep "Mavericks" | sed 's/^[[ \t]]*//;s/[[ \t]]*$//;s/*//' | cut -c 2- | sort)
    fi

This sorts the results and therefore ensures the highest i.e. newest version is indeed the last line returned.

Command Line Tools for Xcode-12.4
Command Line Tools for Xcode-12.5
Command Line Tools for Xcode-12.5
Command Line Tools for Xcode-13.2
rtrouton commented 1 year ago

I tested and wasn't able to reproduce on macOS Monterey 12.6, but I was able to reproduce this on macOS Big Sur 11.7. This change looks like a sensible fix to the problem, so I've added it: https://github.com/rtrouton/rtrouton_scripts/commit/bab5660deb14489afd8d50ff32ce10b28c9b78da