Last active
August 1, 2025 06:22
-
-
Save lopesivan/6f638631552ed7090e2236cd83358028 to your computer and use it in GitHub Desktop.
update-alternatives
This file contains hidden or 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
| #!/usr/bin/env bash | |
| test -n "$DEBUG" && set -x | |
| # __ __ ___ | |
| # /\ \\ \ /'___`\ | |
| # \ \ \\ \ /\_\ /\ \ | |
| # \ \ \\ \_\/_/// /__ | |
| # \ \__ ,__\ // /_\ \ | |
| # \/_/\_\_//\______/ | |
| # \/_/ \/_____/ | |
| # Algoritimos | |
| # | |
| # | |
| # Author: Ivan Lopes | |
| # Mail: ivan@42algoritmos.com.br | |
| # Site: http://www.42algoritmos.com.br | |
| # License: gpl | |
| # Phone: +1 561 801 7985 | |
| # Language: Shell Script | |
| # File: u.sh | |
| # Date: Sex 03 Ago 2018 03:39:01 -03 | |
| # Description: | |
| # ---------------------------------------------------------------------------- | |
| # Modo strict | |
| set -euo pipefail | |
| # ---------------------------------------------------------------------------- | |
| ############################################################################## | |
| ############################################################################## | |
| ############################################################################## | |
| # ---------------------------------------------------------------------------- | |
| versions=( | |
| 5 6 7 8 | |
| ) | |
| let i=50 | |
| for v in ${versions[*]}; do | |
| echo -e '\e[1;49;36m'gcc-${v}'\e[0m' >&2 | |
| echo -n sudo update-alternatives | |
| echo -n " "--install /usr/bin/gcc gcc /usr/bin/gcc-$v $i | |
| dpkg -L gcc-$v g++-$v| | |
| awk -F'/bin/' '/.*bin\// {print $2}' | | |
| while read line; do | |
| [[ "$line" == "gcc-$v" ]] && continue | |
| cmd=${line%-*} | |
| echo -n " "--slave /usr/bin/$cmd $cmd /usr/bin/$line | |
| done | |
| echo | |
| let i=$((i + 10)) | |
| done | |
| # ---------------------------------------------------------------------------- | |
| exit 0 |
Updated for 24.04 with gcc versions 13 & 14 and handled separate packages for /usr/bin/x86_64-linux-gnu-gcc & /usr/bin/x86_64-linux-gnu-g++:
versions=(13 14)
i=10
for v in ${versions[*]}; do
echo -e '\e[1;49;36m'"gcc-${v}"'\e[0m' >&2
cmd_str_install="sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$v $i\n"
cmd_str_slaves=$(
dpkg -L "gcc-$v" "g++-$v" "gcc-$v-x86-64-linux-gnu" "g++-$v-x86-64-linux-gnu" |
awk -F'/bin/' '/.*bin\// {print $2}' |
while read -r line; do
[[ "$line" == "gcc-$v" ]] && continue
cmd=${line%-*}
echo " --slave /usr/bin/$cmd $cmd /usr/bin/$line"
done
)
# remove trailing newline and backslash split each slave section
echo -ne "$cmd_str_install$cmd_str_slaves" | sed -z 's/\n$//' | sed -z 's/\n/ \\\n/g'
echo
i=$((i + 10))
done
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this, very useful! Helped me fix an issue where I thought I was building python with gcc 10, but because the generated python make file used
x86_64-linux-gnu-gccand notgcc, it was actually building with gcc 9...On Ubuntu 20.04, with gcc9 and gcc10, I refactored this and generated the command to use via:
It generated: