Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script - determine vendor and install system (apt-get, yum etc)

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

like image 658
Dr.Avalanche Avatar asked Oct 20 '13 13:10

Dr.Avalanche


People also ask

Is Yum same as apt-get?

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.

How do you check bash is installed or not?

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.


1 Answers

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.

like image 74
Sebastien Avatar answered Sep 26 '22 20:09

Sebastien