Files
common/debian/usr/local/sbin/install_pkg
T

71 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
# Ver: 1.2 by Endial Fang (endial@126.com)
#
# shell 执行参数,分别为 -e(命令执行错误则退出脚本) -u(变量未定义则报错) -x(打印实际待执行的命令行)
set -eu
# 检查用户权限
if [ "$EUID" -ne 0 ]; then
echo "Error: This script must be run as root."
exit 1
fi
print_usage() {
echo "Usage: install_pkg <PACKAGE-NAME>"
echo ""
echo "Download and install packages"
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit."
echo ""
echo "Examples:"
echo " - Install bash & curl"
echo " \$ install_pkg bash curl"
echo ""
}
if [ $# -lt 1 ]; then
print_usage
exit 1
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
print_usage
exit 0
;;
*)
break
;;
esac
done
retry=0
max=2
export DEBIAN_FRONTEND=noninteractive &&
until [ $retry -gt $max ]; do
set +e
(
echo "Update and install packages..." &&
apt-get update &&
apt-get install -y --no-install-recommends $*
)
CODE=$?
set -e
if [ $CODE -eq 0 ]; then
break
fi
if [ $retry -eq $max ]; then
echo "Failed to install packages after $max retries."
exit $CODE
fi
echo "apt failed, retrying"
retry=$(($retry + 1))
done
apt-get clean || :
rm -rf /var/lib/apt/lists/* /var/cache/apt/archives || :
echo "Installation completed successfully."