用于安装 apt-get 和 yum 的通用 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21421120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A universal bash script for installing with apt-get and yum
提问by Justin
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:
我试着写一个简单的bash的包装,其抽象yum
和apt-get
。基本上,我们可以做一些事情,universal-install curl
这是我到目前为止所拥有的:
# universal-install
package=
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?
是否有任何错误或改进/优化可以进行?
回答by Michael Kropat
Take a look at how pacaptdetects the OS:
# Detect package type from /etc/issue
_found_arch() {
local _ostype=""
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.
如您所见,它首先检查/etc/issue
,然后失败,脚本为每个包管理器查找关联的可执行文件。
But heck, why not just use pacapt, instead of rolling your own?
但是,见鬼,为什么不直接使用pacapt,而不是使用自己的呢?
回答by DopeGhoti
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: if [ "" == "`which unzip`" ]; then echo "Unzip Not Found"; if [ -n "`which apt-get`" ]; then apt-get -y install unzip ; elif [ -n "`which yum`" ]; then yum -y install unzip ; fi ; fi
PACKAGE"
exit 1
fi
$VERB ""
exit $?
Slightly better would to to look at /etc/issue
to see what your distribution is and behave accordingly.
稍微好一点的是看看/etc/issue
你的分布是什么,并做出相应的行为。
回答by Dekel
I was looking for a one-liner to install a package and couldn't find any so this was my final version:
我正在寻找一个单线来安装一个包,但找不到,所以这是我的最终版本:
#!/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 "##代码##" "$@"
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
)
It's specific for the
unzip
package, but can be altered to any other package that is available on apt-get/yum.
它特定于
unzip
包,但可以更改为 apt-get/yum 上可用的任何其他包。
Hope this will help someone :)
希望这会帮助某人:)