Distribution dependent package installation script

December 27, 2017 596 views
VPN

Hi,

I've been searching the webs for a solution to my issue and have not come across something solid that works for me. Any help is appreciated.

I would like to install a package (In this case openconnect) using a script that will install it on any linux distribution depending on the OS type.

I know in Debian derivatives I can issue apt-get install openconnect
I know in RHEL derivatives I can issue yum install openconnect
In Fedora I can issue dnf install openconnect

However, I'd like my script to identify the distribution it is running on and use the appropriate package management tool to install it. I do not want my script to work on every distribution, just Debian, Kali, Ubuntu, Fedora, and CentOS.

1 Answer

Here's an idea — hopefully you can make use of it.

You can create a Bash script containing OS check. Probably the best idea would be to parse /etc/os-release file which contains many information about your OS.
You can run an cat command to check file's content.

  • cat /etc/os-release
Output form Fedora 27
NAME=Fedora VERSION="27 (Workstation Edition)" ID=fedora VERSION_ID=27 PRETTY_NAME="Fedora 27 (Workstation Edition)" ANSI_COLOR="0;34" ...

The best idea is to parse either Name or ID, as Pretty Name is prone to change when upgrading.
There are several ways to parse key-value pairs. For example, check out awk.

Once you have distro extracted, you can put it in a if statement:

# parsing /etc/os-release...
if [ $os = "ubuntu" ]; then
       # install packages
fi
Have another answer? Share your knowledge.