Last active
December 18, 2016 19:13
-
-
Save cjwelborn/1caa8a9d3ec697608fb6b70f3524cb9d to your computer and use it in GitHub Desktop.
BASH function to make the user select a directory in $PATH.
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
function select_path { | |
# BASH function to make the user select a directory in $PATH. | |
# Outputs the path on success, returns 1 on error, and 2 when | |
# no path is selected. | |
# Arguments: | |
# $1 : Prompt for the select menu. | |
# Default: "Choose the installation path:" | |
# Example usage: | |
# if mypath="$(select_path)"; then | |
# echo "Success: $mypath" | |
# else | |
# echo "No path selected." | |
local pathdirs=($(printf "%s" "${PATH//:/$'\n'}" | sort)) | |
((${#pathdirs} > 0)) || { | |
printf "Failed to find \$PATH directories!\n" 1>&2 | |
return 1 | |
} | |
PS3=$'\n'"Type \`c\` to cancel."$'\n'"${1:-Choose the installation path:} " | |
local usepath | |
select usepath in "${pathdirs[@]}"; do | |
case "${#usepath}" in | |
0 ) | |
return 2 | |
;; | |
* ) | |
printf "%s" "$usepath" | |
break | |
;; | |
esac | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment