common.sh 518 B

123456789101112131415161718192021222324252627
  1. #!/bin/bash
  2. RED='\033[0;31m'
  3. GREEN='\033[0;32m'
  4. YELLOW='\033[0;33m'
  5. MAGENTA='\033[0;35m'
  6. RESET='\033[0m'
  7. function execute_command() {
  8. command="$1"
  9. message="$2"
  10. exit_on_failure=${3:-true}
  11. echo "${GREEN}执行命令:${command}${RESET}"
  12. eval "${command}"
  13. status=$?
  14. if [[ ${status} -ne 0 ]]; then
  15. echo "${RED}错误:${message}${RESET}"
  16. if [[ "$exit_on_failure" == "true" ]]; then
  17. exit ${status}
  18. else
  19. return ${status}
  20. fi
  21. fi
  22. }