2014-01-29 75 views
3

我想写一个简单的bash包装,其摘要yumapt-get。基本上,所以我们可以做这样的事情universal-install curl这是我到目前为止有:通用bash脚本安装apt-get和yum

# 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 

是否有可以由任何错误或改进/优化?

+0

潜在的问题是分布不同命名他们的所有软件包。 Debian'apt-get package'很可能不会与Redhat'yum install软件包具有相同的名称' –

+0

Yah,让我们忽略软件包名称的差异。不是我可以自动处理的东西,我知道。如果有一个服务(api)为最常见的发行版返回了软件包名称,那真的很酷。 – Justin

回答

1

如果你打算这样做,为什么要求用户告诉脚本使用哪个工具?

#!/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 $? 

稍好一点的去看/etc/issue看看你的发行版本是什么,并据此行事。

+0

?他不要求用户告诉脚本中哪个地方使用哪个工具。也不要使用'which' – BroSlow

+0

有哪些与我应该知道的'哪个'有什么不同?询问头脑想知道。 – DopeGhoti

+1

是的,不要使用'which'来检查命令是否存在,这是为什么:http://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a -bash-script – Justin

4

看看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 
} 

正如你可以看到它首先检查/etc/issue,则没有脚本查找每个包管理器相关的可执行文件。

但是,为什么不只是使用pacapt,而不是滚动自己的?

+1

+1表示最后一行。结束了做到这一点,哈哈。 –

0

命令lsb_release你能帮忙吗?

1

我一直在寻找一个班轮安装一个软件包,但没有找到任何所以这是我的最终版本:

​​

这是特定的unzip包,但可以改变apt-get/yum上可用的任何其他软件包。

希望这会帮助别人:)

跳转右出我
+0

谢谢 - 简单但有效。我不得不稍微扩展你的答案:https://stackoverflow.com/a/46254592/65706 –

0
#!/bin/bash 
    # file: src/bash/aspark-starter/install-prerequisites-for-aspark-starter.sh 
    # caveat package names are for Ubuntu !!! 
    set -eu -o pipefail # fail on error , debug all lines 

    # run as root 
    [ "${USER:-}" = "root" ] || exec sudo "$0" "[email protected]" 

    echo "=== $BASH_SOURCE on $(hostname -f) at $(date)" >&2 


    echo installing the must-have pre-requisites 
    while read -r p ; do 
     if [ "" == "`which $p`" ]; 
     then echo "$p Not Found"; 
      if [ -n "`which apt-get`" ]; 
      then apt-get install -y $p ; 
      elif [ -n "`which yum`" ]; 
      then yum -y install $p ; 
      fi ; 
     fi 
    done < <(cat << "EOF" 
     perl 
     zip unzip 
     exuberant-ctags 
     mutt 
     libxml-atom-perl 
     postgresql-9.6 
     libdbd-pgsql 
     curl 
     wget 
     libwww-curl-perl 
    EOF 
    ) 

    echo installing the nice-to-have pre-requisites 
    echo you have 5 seconds to proceed ... 
    echo or 
    echo hit Ctrl+C to quit 
    echo -e "\n" 
    sleep 6 

    echo installing the nice to-have pre-requisites 
    while read -r p ; do 
     if [ "" == "`which $p`" ]; 
     then echo "$p Not Found"; 
      if [ -n "`which apt-get`" ]; 
      then apt-get install -y $p ; 
      elif [ -n "`which yum`" ]; 
      then yum -y install $p ; 
      fi ; 
     fi 
    done < <(cat << "EOF" 
     tig 
    EOF 
    )