Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A universal bash script for installing with apt-get and yum

I'm trying to write a simple bash wrapper which abstracts yum and apt-get. Basically so we can do something like universal-install curl Here is what I have so far:

# universal-install
package=$1
apt=`command -v apt-get`
yum=`command -v yum`

if [ -n "$apt" ]; then
    apt-get update
    apt-get -y install $package
elif [ -n "$yum" ]; then
    yum -y install $package
else
    echo "Err: no path to apt-get or yum" >&2;
    exit 1;
fi

Are there any errors or improvements/optimizations that can be made?

like image 282
Justin Avatar asked Jan 29 '14 02:01

Justin


2 Answers

Take a look at how pacapt detects the OS:

# Detect package type from /etc/issue
_found_arch() {
  local _ostype="$1"
  shift
  grep -qis "$*" /etc/issue && _OSTYPE="$_ostype"
}

# Detect package type
_OSTYPE_detect() {
  _found_arch PACMAN "Arch Linux" && return
  _found_arch DPKG   "Debian GNU/Linux" && return
  _found_arch DPKG   "Ubuntu" && return
  _found_arch YUM    "CentOS" && return
  _found_arch YUM    "Red Hat" && return
  _found_arch YUM    "Fedora" && return
  _found_arch ZYPPER "SUSE" && return

  [[ -z "$_OSTYPE" ]] || return

  # See also https://github.com/icy/pacapt/pull/22
  # Please not that $OSTYPE (which is `linux-gnu` on Linux system)
  # is not our $_OSTYPE. The choice is not very good because
  # a typo can just break the logic of the program.
  if [[ "$OSTYPE" != "darwin"* ]]; then
    _error "Can't detect OS type from /etc/issue. Running fallback method."
  fi
  [[ -x "/usr/bin/pacman" ]]           && _OSTYPE="PACMAN" && return
  [[ -x "/usr/bin/apt-get" ]]          && _OSTYPE="DPKG" && return
  [[ -x "/usr/bin/yum" ]]              && _OSTYPE="YUM" && return
  [[ -x "/opt/local/bin/port" ]]       && _OSTYPE="MACPORTS" && return
  command -v brew >/dev/null           && _OSTYPE="HOMEBREW" && return
  [[ -x "/usr/bin/emerge" ]]           && _OSTYPE="PORTAGE" && return
  [[ -x "/usr/bin/zypper" ]]           && _OSTYPE="ZYPPER" && return
  if [[ -z "$_OSTYPE" ]]; then
    _error "No supported package manager installed on system"
    _error "(supported: apt, homebrew, pacman, portage, yum)"
    exit 1
  fi
}

As you can see it first checks /etc/issue, then failing that the script looks for the associated executable file for each package manager.

But heck, why not just use pacapt, instead of rolling your own?

like image 76
Michael Kropat Avatar answered Sep 19 '22 01:09

Michael Kropat


If you're going to do this, why require the user to tell the script which tool to use?

#!/bin/bash
# Find our package manager
if VERB="$( which apt-get )" 2> /dev/null; then
   echo "Debian-based"
elif VERB="$( which yum )" 2> /dev/null; then
   echo "Modern Red Hat-based"
elif VERB="$( which portage )" 2> /dev/null; then
   echo "Gentoo-based"
elif VERB="$( which pacman )" 2> /dev/null; then
   echo "Arch-based"
else
   echo "I have no idea what I'm doing." >&2
   exit 1
fi
if [[ 1 -ne $# ]]; then
   echo "Syntax: $0 PACKAGE"
   exit 1
fi
$VERB "$1"
exit $?

Slightly better would to to look at /etc/issue to see what your distribution is and behave accordingly.

like image 22
DopeGhoti Avatar answered Sep 20 '22 01:09

DopeGhoti