#!/bin/sh # Ver: 1.3 by Endial Fang (endial@126.com) # # shell 执行参数,分别为 -e(命令执行错误则退出脚本) -u(变量未定义则报错) -x(打印实际待执行的命令行) set -eu # 检查用户权限 if [ "$(id -u)" -ne 0 ]; then echo "Error: This script must be run as root." exit 1 fi print_usage() { echo "Usage: install_pkg " 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 until [ $retry -gt $max ]; do set +e ( echo "Update and install packages..." apk update --no-cache apk add --no-cache "$@" ) 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 "APK failed, retrying" retry=$(($retry + 1)) done # rm -r /var/cache/apk/* /root/.cache /tmp/* || :