I'm writing a shell script for use on various linux platforms. Part of the script installs a couple of packages. How can I determine the linux vendor and default system install mechanism, for example Debian/Ubuntu has apt-get/apt, Fedora has yum and so on...
Thanks in advance
Installing is basically the same, you do 'yum install package' or 'apt-get install package' you get the same result. Yum automatically refreshes the list of packages, whilst with apt-get you must execute a command 'apt-get update' to get the fresh packages. Another difference is upgrading all the packages.
To find my bash version, run any one of the following command: Get the version of bash I am running, type: echo "${BASH_VERSION}" Check my bash version on Linux by running: bash --version. To display bash shell version press Ctrl + x Ctrl + v.
You don't really need to check for vendor as they may decide to change packaging system (unlikely but conceptually, you would have to ensure that for each distro you test for, you try the right package manager command). All you have to do is test for the installation itself:
YUM_CMD=$(which yum)
APT_GET_CMD=$(which apt-get)
OTHER_CMD=$(which <other installer>)
and then possibly sort them in your preference order:
if [[ ! -z $YUM_CMD ]]; then
yum install $YUM_PACKAGE_NAME
elif [[ ! -z $APT_GET_CMD ]]; then
apt-get $DEB_PACKAGE_NAME
elif [[ ! -z $OTHER_CMD ]]; then
$OTHER_CMD <proper arguments>
else
echo "error can't install package $PACKAGE"
exit 1;
fi
you can take a look at how gentoo (or framework similar to yocto or openembedded) provide approach to even get the source code (with wget) and build from scratch if you want a failsafe script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With