5

I have the following install script for ubuntu :

#!/bin/bash

sudo apt update
sudo apt full-upgrade -y
sudo apt install jq
sudo apt autoclean -y
sudo apt autoremove

will the following work under fedora, red hat, mageia or other rpm-based distros

...or does the syntax have to change more?

#!/bin/bash

sudo rpm update
sudo rpm full-upgrade -y
sudo rpm install jq
sudo rpm autoclean -y
sudo rpm autoremove

also can I do something to the effect of the following? :

#!/bin/bash
if [ $(command -v yum) ]
then
    sudo yum update
    sudo yum full-upgrade -y
    sudo yum install jq
    sudo yum autoclean -y
    sudo yum autoremove
else
    sudo rpm update
    sudo rpm full-upgrade -y
    sudo rpm install jq
    sudo rpm autoclean -y
    sudo rpm autoremove
fi
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
tatsu
  • 306
  • 3
  • 16

2 Answers2

14

rpm is mostly equivalent to dpkg, not apt; the apt equivalent is yum (on RHEL and CentOS up to release 7), or dnf (on Fedora, and RHEL and CentOS starting with release 8), or zypper (on SuSE). For your specific commands:

sudo dnf distro-sync
sudo dnf install jq
sudo dnf clean all
sudo dnf autoremove

or

sudo yum upgrade
sudo yum install jq
sudo yum clean all

(This works because jq is packaged under the same name in both cases. This isn’t always true; a given piece of software can be packaged under different names in different distributions or even different releases of a given distribution.)

See the Pacman Rosetta and the Ubuntu RHEL migration guide for details.

You might want to look into configuration management tools instead, they will help you abstract the differences away (or at least, deal with them more robustly).

Your if [ $(command -v yum) ] test is flawed because yum can be installed on Debian derivatives (including Ubuntu); its presence doesn’t mean it’s the package manager. You should probably detect the running operating system and base your choice on that; see How can I reliably get the operating system's name? for details.

Stephen Kitt
  • 434,908
  • aw shucks package name can change? whyyyy? – tatsu Apr 16 '19 at 15:31
  • who has dnf? redhat? and I assume yum is more towards mageia and fedora? – tatsu Apr 16 '19 at 15:32
  • 2
    Because they’re different distribution, with different packaging practices, naming rules, etc. – Stephen Kitt Apr 16 '19 at 15:32
  • 1
    yum is used in RHEL up to version 7, CentOS up to version 7, and probably other RPM-based distributions; dnf is used in Fedora, and RHEL 8 and later. – Stephen Kitt Apr 16 '19 at 15:33
  • huh, fedora doesn't use yum? – tatsu Apr 16 '19 at 15:34
  • 1
    No, it hasn’t for quite a while. – Stephen Kitt Apr 16 '19 at 15:36
  • Even if another distribution uses the same package manager, it doesn't always mean that an application will be packaged under the same name or even exist. For example, there may or may not be a "firefox" package in your particular release of debian. – gmatht Apr 17 '19 at 06:00
  • There is also Zypper from SuSE, which sadly has a bit of market share, especially around SAP. –  Apr 17 '19 at 07:22
  • @Morfildur yup, and the equivalent commands for Zypper are listed in the Pacman Rosetta. – Stephen Kitt Apr 17 '19 at 08:06
  • @tatsu Package names do not only change between package managers, but even between distributions. Debian and Ubuntu for example both use apt, but do not agree on all package names. – allo Apr 17 '19 at 08:09
  • @allo thanks for the information. that is just insanity. I can't be the only one who thinks this. we must create an effort to unify names. – tatsu Apr 17 '19 at 08:18
  • 1
    @tatsu You might find this educational: https://xkcd.com/927/ –  Apr 17 '19 at 09:52
  • @Morfildur haha, yeah. although in this case I'm not advocating creating everything new, just knocking at doors and bugging people over exsisting repos. – tatsu Apr 17 '19 at 10:54
5

No, the options and arguments to apt and yum are different, so are package names in a lot of cases.

You also seem to be getting rpm and yum confused, yum is the equivalent of apt, rpm is the equivalent of dpkg. dpkg is the backend for apt, rpm is the backend for yum.

You will have to look at the man pages for both apt and yum to find the equivalent options. Alternativly you could look at a configuration management tool like puppet which will abstract a lot of OS differences between distros, but this may be overkill for what you're doing.