bashcommand-linerpmdeb

Find out which package I have to install on a Linux system


I made a bash script to install a software package on a Linux system. There are 4 packages I can use to install the software:

  1. x86.deb
  2. x86.rpm
  3. x86_64.deb
  4. x86_64.rpm

I know when to install which package on which Linux server manually, but I would like to find out "automatically" (in my bash script) which one I have to install.

Is there any command to find out? I already know there is a way to find out the architecture (32-bit or 64-bit) via the "arch" command, but I do not know how to find out which package I need.


Solution

  • uname -m or arch gives you the architecture (x86_64 or similar).

    You can probably figure out whether your system is based on RPM or DEB (e.g. Ubuntu is DEB-based) by asking both variants which package installed /bin/ls:

    dpkg -S /bin/ls
    

    will print

    coreutils: /bin/ls
    

    on a DEB-based system.

    rpm -q -f /bin/ls
    

    will print

    coreutils-5.97-23.el5_6.4
    

    on an RPM-based system (with probably different version numbers).

    On the "wrong" system each of these will give an error message instead.

    if dpkg -S /bin/ls >/dev/null 2>&1
    then
      case "$(arch)" in
        x86_64)
          sudo dpkg -i x86_64.deb;;
        i386)
          sudo dpkg -i x86.deb;;
        *)
          echo "Don't know how to handle $(arch)"
          exit 1
          ;;
      esac
    elif rpm -q -f /bin/ls >/dev/null 2>&1
    then
      case "$(arch)" in
        x86_64)
          sudo rpm -i x86_64.rpm;;
        i386)
          sudo rpm -i x86.rpm;;
        *)
          echo "Don't know how to handle $(arch)"
          exit 1
          ;;
      esac
    else
      echo "Don't know this package system (neither RPM nor DEB)."
      exit 1
    fi
    

    Of course, all this only makes sense in case you know what to do then, i.e. if you know which package is to be installed on which package system with which architecture.