Ask Your Question
3

how to check if system is rpm or debian based

asked 2014-07-05 10:09:59 -0600

avaj gravatar image

I m writing a script for which i need to know whether my system is rpm or debian based.

I searched but couldn't find any solution for the same. I don't want distro or release names.

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
3

answered 2014-07-07 15:43:15 -0600

updated 2014-07-12 11:29:27 -0600

To prevent the corner case mentioned by skytux, I'd do it like this:

/usr/bin/rpm -q -f /usr/bin/rpm >/dev/null 2>&1

If the exit code is 0, the system is probably RPM based.


Other usefull commands which you can try are:

  • /usr/bin/rpm -q -f /usr/bin/dpkg
  • /usr/bin/dpkg --search /usr/bin/dpkg
  • /usr/bin/dpkg --search /usr/bin/rpm

I've done tests on:

  • Fedora 20 x86_64

    [joe@fedora20 ~]$ rpm -q dpkg
    dpkg-1.16.15-1.fc20.x86_64
    
  • Ubuntu 14.04

    joe@ubuntu1404 ~$ dpkg --status rpm
    Package: rpm
    Status: install ok installed
    Architecture: amd64
    Version: 4.11.1-3
    

Here are the results of the tests (with truncated error message for brevity):

    FEDORA 20 x86_64                           ║     UBUNTU 14.04 AMD64
═══════════════════════════════════════════════╬════════════════════════════════════════════
[j@f ~]$ /usr/bin/rpm -q -f /usr/bin/rpm       ║ j@u:~$ /usr/bin/rpm -q -f /usr/bin/rpm
rpm-4.11.2-2.fc20.x86_64                       ║ file /usr/bin/rpm is not owned by ...
[j@f ~]$ echo $?                               ║ j@u:~$ echo $?
0                                              ║ 1
                                               ║
[j@f ~]$ /usr/bin/rpm -q -f /usr/bin/dpkg      ║ j@u ~$ /usr/bin/rpm -q -f /usr/bin/dpkg
dpkg-1.16.15-1.fc20.x86_64                     ║ file /usr/bin/dpkg is not owned by ...
[j@f ~]$ echo $?                               ║ j@u ~$ echo $?
0                                              ║ 1
                                               ║
[j@f ~]$ /usr/bin/dpkg --search /usr/bin/dpkg  ║ j@u ~$ /usr/bin/dpkg --search /usr/bin/dpkg
dpkg-query: no path found matching pattern ... ║ dpkg: /usr/bin/dpkg
[j@f ~]$ echo $?                               ║ j@u ~$ echo $?
1                                              ║ 0
                                               ║
[j@f ~]$ /usr/bin/dpkg --search /usr/bin/rpm   ║ j@u ~$ /usr/bin/dpkg --search /usr/bin/rpm
dpkg-query: no path found matching pattern ... ║ rpm: /usr/bin/rpm
[j@f ~]$ echo $?                               ║ j@u ~$ echo $?
1                                              ║ 0
edit flag offensive delete link more

Comments

Nice, I liked it!

skytux gravatar imageskytux ( 2014-07-07 19:21:07 -0600 )edit

@Cristian Ciupitu - Doesn't work on Ubuntu machine w/ rpm installed. echo $? returns 1.

abadrinath gravatar imageabadrinath ( 2014-07-08 20:54:50 -0600 )edit

So it means that it worked correctly.

skytux gravatar imageskytux ( 2014-07-09 04:35:43 -0600 )edit

Oops, hahah I meant 0.

abadrinath gravatar imageabadrinath ( 2014-07-09 04:45:41 -0600 )edit

@hello, what about /usr/bin/rpm -q -f /usr/bin/dpkg or /usr/bin/dpkg --search /usr/bin/rpm?

Cristian Ciupitu gravatar imageCristian Ciupitu ( 2014-07-09 13:19:52 -0600 )edit
1

answered 2014-07-06 00:04:02 -0600

abadrinath gravatar image

updated 2014-07-09 04:50:57 -0600

Try which rpm and which gdebi to determine the type of installation. For example:

#!/bin/bash

if which gdebi > /dev/null 2> /dev/null
then echo This is an debian system
else echo This is a rpm system
fi

If you are looking for a C alternative, here:

#include <stdio.h>
#include <stdlib.h>


int main( int argc, char *argv[] )
{

  FILE *fp;
  int status;
  char path[1035];

  /* Open the command for reading. */
  fp = popen("/usr/bin/which gdebi", "r");
  if (fp == NULL) {
    printf("This is a rpm system\n" );
    exit;
  }
  else{
    printf("This is an debian system\n");
    exit;
}

  return 0;
}

EDIT: I don't think gdebi can be installed for Fedora and RPM based distros. So, if you have gdebi, you have .deb.

edit flag offensive delete link more

Comments

1

And what happens if the Debian system has the package rpm installed on it?

skytux gravatar imageskytux ( 2014-07-07 09:57:27 -0600 )edit

i tried the above code and its not working. Any command u search for, the code will always returns the else part.

avaj gravatar imageavaj ( 2014-07-10 10:37:43 -0600 )edit

@avaj - ?? What do u mean? It works fine on my Ubuntu 14.04 & F20.

abadrinath gravatar imageabadrinath ( 2014-07-10 22:49:44 -0600 )edit

I have Ubuntu 14.04 and rpm installed onto it but the code always return the else part of the code.

avaj gravatar imageavaj ( 2014-07-12 08:57:02 -0600 )edit
1

answered 2015-06-07 14:23:37 -0600

c0rp gravatar image

Check release files:

if [ -f /etc/debian_version ]; then
    echo "This is debian based distro"
elif [ -f /etc/redhat-release ]; then
    echo "This is RedHat based distro"
else
    echo "This is something else"
fi
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2014-07-05 10:09:59 -0600

Seen: 18,027 times

Last updated: Jun 07 '15