Skip to content

Instantly share code, notes, and snippets.

@LozanoMatheus
Last active December 31, 2020 20:29
Show Gist options
  • Save LozanoMatheus/68dc8337b08a94329044af175f1d8279 to your computer and use it in GitHub Desktop.
Save LozanoMatheus/68dc8337b08a94329044af175f1d8279 to your computer and use it in GitHub Desktop.
Installing Bash on Ubuntu / Centos / Alpine with --enable-net-redirections
#!/usr/bin/env sh
LINUX_DISTRO="$(. /etc/os-release ; echo ${ID})"
BASH_VERSION="bash-5.0"
SOURCE_DIR="/var/opt/"
prereqs_centos(){
install_bash
yum install -y -q gcc make &> /dev/null
yum clean all &> /dev/null
rm -rf /var/cache/yum/
}
prereqs_ubuntu(){
install_bash
apt-get update &> /dev/null
apt-get -y install gcc make &> /dev/null
./configure --enable-net-redirections
}
prereqs_alpine(){
install_bash
apk add --no-cache gcc g++ make &> /dev/null
}
install_bash(){
cd "${SOURCE_DIR}/${BASH_VERSION}"
./configure --enable-net-redirections ${1}
make
make test
make install
/usr/local/bin/bash --version
}
case "${LINUX_DISTRO}" in
alpine)
prereqs_alpine
wget -q "http://git.savannah.gnu.org/cgit/bash.git/snapshot/${BASH_VERSION}.tar.gz" -O- | tar -xz -C "${SOURCE_DIR}"
install_bash --without-bash-malloc
;;
centos)
prereqs_centos
curl -sL "http://git.savannah.gnu.org/cgit/bash.git/snapshot/${BASH_VERSION}.tar.gz" | tar -xz -C "${SOURCE_DIR}"
install_bash
;;
ubuntu)
prereqs_ubuntu
curl -sL "http://git.savannah.gnu.org/cgit/bash.git/snapshot/${BASH_VERSION}.tar.gz" | tar -xz -C "${SOURCE_DIR}"
install_bash
;;
*)
echo "${LINUX_DISTRO} is not supported"
;;
esac
@elreydetoda
Copy link

I believe sh has case statements. Is there a reason why you used if-elif instead of case statements? That would optimize ( minor, but still ) to only one comparison compared to 3 ( at worst ).

Also, a cool trick I learned ( by I think reading digitalocean's logging script ) about the /etc/*release files is that you can source those files and then just echo/printf that variable. Because they are all in variable assignment format, so instead of parsing for it just do something like LINUX_DISTRO="$(source /etc/*release ; echo "${ID}" )"

Since that is a subshell no variables should be introduced to your script ( IIRC ), and you don't have to parse anything out + echo/printf are builtins ( as well as source ( or the alias of . if you want to be minimal ) ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment