Guest User

Untitled

a guest
Nov 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4. RED="33[1;31m"
  5. GREEN="33[1;32m"
  6. YELLOW="33[1;33m"
  7. NOCOLOR="33[0m"
  8.  
  9.  
  10. function show_success {
  11.  
  12. echo -e "${GREEN}Success.${NOCOLOR}n"
  13.  
  14. }
  15.  
  16.  
  17. function show_error_and_exit {
  18.  
  19. echo -e "${RED}An error occured.${NOCOLOR}n"
  20.  
  21. exit "$1"
  22.  
  23. }
  24.  
  25. function error_handler {
  26.  
  27. if [[ $1 -ne 0 ]];
  28. then
  29.  
  30. show_error_and_exit "$2"
  31.  
  32. else
  33.  
  34. show_success
  35.  
  36. fi
  37.  
  38. }
  39.  
  40.  
  41. echo -e "n${GREEN}Step 1: configure packages${NOCOLOR}"
  42. echo -e "${YELLOW}dpkg --configure -a${NOCOLOR}"
  43.  
  44. sudo dpkg --configure -a
  45.  
  46. error_handler $? 1
  47.  
  48.  
  49. echo -e "${GREEN}Step 2: fix broken dependencies${NOCOLOR}"
  50. echo -e "${YELLOW}apt-get install --fix-broken${NOCOLOR}"
  51.  
  52. sudo apt-get install --fix-broken
  53.  
  54. error_handler $? 2
  55.  
  56.  
  57. echo -e "${GREEN}Step 3: update cache${NOCOLOR}"
  58. echo -e "${YELLOW}apt-get update${NOCOLOR}"
  59.  
  60. sudo apt-get update
  61.  
  62. error_handler $? 3
  63.  
  64.  
  65. echo -e "${GREEN}Step 4: upgrade packages${NOCOLOR}"
  66. echo -e "${YELLOW}apt-get upgrade${NOCOLOR}"
  67.  
  68. sudo apt-get upgrade
  69.  
  70. error_handler $? 4
  71.  
  72.  
  73. echo -e "${GREEN}Step 5: upgrade distribution${NOCOLOR}"
  74. echo -e "${YELLOW}apt-get dist-upgrade${NOCOLOR}"
  75.  
  76. sudo apt-get dist-upgrade
  77.  
  78. error_handler $? 5
  79.  
  80.  
  81. echo -e "${GREEN}Step 6: remove unused packages${NOCOLOR}"
  82. echo -e "${YELLOW}apt-get --purge autoremove${NOCOLOR}"
  83.  
  84. sudo apt-get --purge autoremove
  85.  
  86. error_handler $? 6
  87.  
  88.  
  89. echo -e "${GREEN}Step 7: clean up${NOCOLOR}"
  90. echo -e "${YELLOW}apt-get autoclean${NOCOLOR}"
  91.  
  92. sudo apt-get autoclean
  93.  
  94. error_handler $? 7
  95.  
  96. #!/bin/bash
  97.  
  98. set -e
  99.  
  100. run () {
  101. local task=$1
  102. local desc=$2
  103. shift 2
  104.  
  105. #local rc
  106.  
  107. # More portable would be to use tput instead of literal escape codes
  108. # Avoid uppercase for non-system variables
  109. local red="33[1;31m"
  110. local green="33[1;32m"
  111. local yellow="33[1;33m"
  112. local nocolor="33[0m"
  113.  
  114. printf "${green}Step $task: $desc.${nocolor}n"
  115. printf "${yellow}$@${nocolor}n"
  116.  
  117. if sudo "$@"; then
  118. printf "${green}Success.${nocolor}n"
  119. else
  120. # The fix to capture the failed command's exit code
  121. # was removed by the OP in an edit of the code
  122. # but I'm recording it here for posterity.
  123. #rc=$?
  124. #printf "${red}Failure: $task${nocolor}n"
  125. #return $rc
  126. printf "${red}Failure: $task${nocolor}n"
  127. return $task
  128. fi
  129. }
  130.  
  131. while IFS=: read exit cmd doco; do
  132. run $exit "$doco" $cmd || exit
  133. done <<____HERE
  134. 1:dpkg --configure -a :configure packages
  135. 2:apt-get install --fix-broken :fix broken dependencies
  136. 3:apt-get update :update cache
  137. 4:apt-get upgrade :upgrade packages
  138. 5:apt-get dist-upgrade :upgrade distribution
  139. 6:apt-get --purge autoremove :remove unused packages
  140. 7:apt-get autoclean :clean up
  141. ____HERE
Add Comment
Please, Sign In to add comment