Distribution dependent package installation script


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.

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.

  1. 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