|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | | -#!/usr/bin/env bash |
4 | | - |
5 | 3 | # ------------------------------------------------------------------------------ |
6 | 4 | # CentOS 常用软件一键安装脚本 |
7 | 5 | # @author Zhang Peng |
8 | 6 | # ------------------------------------------------------------------------------ |
9 | 7 |
|
10 | | -# ------------------------------------------------------------------------------ libs |
11 | | -# 装载其它库 |
12 | | -LINUX_SCRIPTS_DIR=$(cd `dirname 0ドル`; pwd) |
| 8 | +# ------------------------------------------------------------------------------ env |
| 9 | + |
| 10 | +# Regular Color |
| 11 | +export ENV_COLOR_BLACK="033円[0;30m" |
| 12 | +export ENV_COLOR_RED="033円[0;31m" |
| 13 | +export ENV_COLOR_GREEN="033円[0;32m" |
| 14 | +export ENV_COLOR_YELLOW="033円[0;33m" |
| 15 | +export ENV_COLOR_BLUE="033円[0;34m" |
| 16 | +export ENV_COLOR_MAGENTA="033円[0;35m" |
| 17 | +export ENV_COLOR_CYAN="033円[0;36m" |
| 18 | +export ENV_COLOR_WHITE="033円[0;37m" |
| 19 | +# Bold Color |
| 20 | +export ENV_COLOR_B_BLACK="033円[1;30m" |
| 21 | +export ENV_COLOR_B_RED="033円[1;31m" |
| 22 | +export ENV_COLOR_B_GREEN="033円[1;32m" |
| 23 | +export ENV_COLOR_B_YELLOW="033円[1;33m" |
| 24 | +export ENV_COLOR_B_BLUE="033円[1;34m" |
| 25 | +export ENV_COLOR_B_MAGENTA="033円[1;35m" |
| 26 | +export ENV_COLOR_B_CYAN="033円[1;36m" |
| 27 | +export ENV_COLOR_B_WHITE="033円[1;37m" |
| 28 | +# Underline Color |
| 29 | +export ENV_COLOR_U_BLACK="033円[4;30m" |
| 30 | +export ENV_COLOR_U_RED="033円[4;31m" |
| 31 | +export ENV_COLOR_U_GREEN="033円[4;32m" |
| 32 | +export ENV_COLOR_U_YELLOW="033円[4;33m" |
| 33 | +export ENV_COLOR_U_BLUE="033円[4;34m" |
| 34 | +export ENV_COLOR_U_MAGENTA="033円[4;35m" |
| 35 | +export ENV_COLOR_U_CYAN="033円[4;36m" |
| 36 | +export ENV_COLOR_U_WHITE="033円[4;37m" |
| 37 | +# Background Color |
| 38 | +export ENV_COLOR_BG_BLACK="033円[40m" |
| 39 | +export ENV_COLOR_BG_RED="033円[41m" |
| 40 | +export ENV_COLOR_BG_GREEN="033円[42m" |
| 41 | +export ENV_COLOR_BG_YELLOW="033円[43m" |
| 42 | +export ENV_COLOR_BG_BLUE="033円[44m" |
| 43 | +export ENV_COLOR_BG_MAGENTA="033円[45m" |
| 44 | +export ENV_COLOR_BG_CYAN="033円[46m" |
| 45 | +export ENV_COLOR_BG_WHITE="033円[47m" |
| 46 | +# Reset Color |
| 47 | +export ENV_COLOR_RESET="$(tput sgr0)" |
| 48 | + |
| 49 | +# status |
| 50 | +export ENV_YES=0 |
| 51 | +export ENV_NO=1 |
| 52 | +export ENV_SUCCEED=0 |
| 53 | +export ENV_FAILED=1 |
| 54 | + |
| 55 | + |
| 56 | +# ------------------------------------------------------------------------------ functions |
| 57 | + |
| 58 | +# 显示打印日志的时间 |
| 59 | +SHELL_LOG_TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") |
| 60 | +# 那个用户在操作 |
| 61 | +USER=$(whoami) |
| 62 | +# 日志路径 |
| 63 | +LOG_PATH=${ENV_LOG_PATH:-/var/log/shell.log} |
| 64 | +# 日志目录 |
| 65 | +LOG_DIR=${LOG_PATH%/*} |
| 66 | + |
| 67 | +createLogFileIfNotExists() { |
| 68 | + if [[ ! -x "${LOG_PATH}" ]]; then |
| 69 | + mkdir -p "${LOG_DIR}" |
| 70 | + touch "${LOG_PATH}" |
| 71 | + fi |
| 72 | +} |
| 73 | + |
| 74 | +redOutput() { |
| 75 | + echo -e "${ENV_COLOR_RED} $@${ENV_COLOR_RESET}" |
| 76 | +} |
| 77 | +greenOutput() { |
| 78 | + echo -e "${ENV_COLOR_B_GREEN} $@${ENV_COLOR_RESET}" |
| 79 | +} |
| 80 | +yellowOutput() { |
| 81 | + echo -e "${ENV_COLOR_YELLOW} $@${ENV_COLOR_RESET}" |
| 82 | +} |
| 83 | +blueOutput() { |
| 84 | + echo -e "${ENV_COLOR_BLUE} $@${ENV_COLOR_RESET}" |
| 85 | +} |
| 86 | +magentaOutput() { |
| 87 | + echo -e "${ENV_COLOR_MAGENTA} $@${ENV_COLOR_RESET}" |
| 88 | +} |
| 89 | +cyanOutput() { |
| 90 | + echo -e "${ENV_COLOR_CYAN} $@${ENV_COLOR_RESET}" |
| 91 | +} |
| 92 | +whiteOutput() { |
| 93 | + echo -e "${ENV_COLOR_WHITE} $@${ENV_COLOR_RESET}" |
| 94 | +} |
| 95 | + |
| 96 | +logInfo() { |
| 97 | + echo -e "${ENV_COLOR_B_GREEN}[INFO] $@${ENV_COLOR_RESET}" |
| 98 | + createLogFileIfNotExists |
| 99 | + echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [INFO] [0ドル] $@" >> "${LOG_PATH}" |
| 100 | +} |
| 101 | +logWarn() { |
| 102 | + echo -e "${ENV_COLOR_B_YELLOW}[WARN] $@${ENV_COLOR_RESET}" |
| 103 | + createLogFileIfNotExists |
| 104 | + echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [WARN] [0ドル] $@" >> "${LOG_PATH}" |
| 105 | +} |
| 106 | +logError() { |
| 107 | + echo -e "${ENV_COLOR_B_RED}[ERROR] $@${ENV_COLOR_RESET}" |
| 108 | + createLogFileIfNotExists |
| 109 | + echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [ERROR] [0ドル] $@" >> "${LOG_PATH}" |
| 110 | +} |
13 | 111 |
|
14 | | -if [[ ! -x ${LINUX_SCRIPTS_DIR}/lib/utils.sh ]]; then |
15 | | - logError "必要脚本库 ${LINUX_SCRIPTS_DIR}/lib/utils.sh 不存在!" |
16 | | - exit 1 |
17 | | -fi |
| 112 | +printInfo() { |
| 113 | + echo -e "${ENV_COLOR_B_GREEN}[INFO] $@${ENV_COLOR_RESET}" |
| 114 | +} |
| 115 | +printWarn() { |
| 116 | + echo -e "${ENV_COLOR_B_YELLOW}[WARN] $@${ENV_COLOR_RESET}" |
| 117 | +} |
| 118 | +printError() { |
| 119 | + echo -e "${ENV_COLOR_B_RED}[ERROR] $@${ENV_COLOR_RESET}" |
| 120 | +} |
18 | 121 |
|
19 | | -source ${LINUX_SCRIPTS_DIR}/lib/utils.sh |
| 122 | +callAndLog () { |
| 123 | + $* |
| 124 | + if [[ $? -eq ${ENV_SUCCEED} ]]; then |
| 125 | + logInfo "$@" |
| 126 | + return ${ENV_SUCCEED} |
| 127 | + else |
| 128 | + logError "$@ EXECUTE FAILED" |
| 129 | + return ${ENV_FAILED} |
| 130 | + fi |
| 131 | +} |
20 | 132 |
|
21 | 133 | # ------------------------------------------------------------------------------ functions |
22 | 134 |
|
|
0 commit comments