Created
May 4, 2023 20:27
-
-
Save jerryorr/9e8abbe082b7aacfb89cc199216a7373 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Regex to capture major and minor versions | |
pattern="^intellijidea([0-9]*)\.([0-9]*)$" | |
# Directory to search is 1st argument | |
directory=$1 | |
function assign_newest () { | |
newest_major=$1 | |
newest_minor=$2 | |
newest_directory=$3 | |
} | |
assign_newest 0 0 '' | |
# Loop through all subdirectories in the specified directory | |
for dir in $directory/*/; do | |
# Get the name of the subdirectory | |
dirname=$(basename $dir) | |
# If the directory name matches the pattern, capture the groups | |
if [[ ${dirname:l} =~ $pattern ]]; then | |
# Convert the capture groups to numbers | |
major=$((match[1])) | |
minor=$((match[2])) | |
if [[ $major -gt $newest_major ]]; then | |
# If the current major is bigger, use it | |
assign_newest $major $minor $dirname | |
elif [[ $major -eq $newest_major && $minor -gt $newest_minor ]]; then | |
# If the current major is the same but minor is bigger, use it | |
assign_newest $major $minor $dirname | |
fi | |
fi | |
done | |
echo $newest_directory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment