Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. ################################################################################
  2. # 8-bit colors
  3. ################################################################################
  4.  
  5. black='\u001b[30m'
  6. red='\u001b[31m'
  7. green='\u001b[32m'
  8. yellow='\u001b[33m'
  9. blue='\u001b[34m'
  10. magenta='\u001b[35m'
  11. cyan='\u001b[36m'
  12. white='\u001b[37m'
  13. bright_black='\u001b[30;1m'
  14. bright_red='\u001b[31;1m'
  15. bright_green='\u001b[32;1m'
  16. bright_yellow='\u001b[33;1m'
  17. bright_blue='\u001b[34;1m'
  18. bright_magenta='\u001b[35;1m'
  19. bright_cyan='\u001b[36;1m'
  20. bright_white='\u001b[37;1m'
  21.  
  22.  
  23. ################################################################################
  24. # Documentation and error messages
  25. ################################################################################
  26.  
  27. echo_info() {
  28. # Echos an informational message.
  29. #
  30. # depends_variable blue
  31. echo -e "${blue}[INFO]: $1"
  32. }
  33.  
  34. echo_warn() {
  35. # Echos a warning message.
  36. #
  37. # depends_variable yellow
  38. echo -e "${yellow}[WARNING]: $1"
  39. }
  40.  
  41. echo_error() {
  42. # Echos an error message.
  43. #
  44. # depends_variable red
  45. echo -e "${red}[ERROR]: $1"
  46. }
  47.  
  48. unsupported_input() {
  49. # Generic error message. Consume herein to standardize the error messages.
  50. # Pass the supported inputs as $1.
  51. #
  52. # depends error_error
  53. echo_error "Unsupported input. This function only supports the following inputs: $1. Exiting..."
  54. }
  55.  
  56. depends() {
  57. # Prints a message explaining a function's dependencies. Consume here to
  58. # standardize the error messages.
  59. # Pass the dependencies as $1.
  60. #
  61. # depends echo_info
  62. echo_info "This function depends on the following functions: $@"
  63. }
  64.  
  65. depends_variable() {
  66. # Prints a message explaining a dependency on a variable. Consume here to
  67. # standardize the error messages.
  68. # Pass the dependencies as $1.
  69. #
  70. # depends echo_info
  71. echo_info "This function depends on the following variables: $@"
  72. }
  73.  
  74. compliments() {
  75. # Prints a message explaining that a function compliments another function.
  76. # Think of complimentary functions as `zip` and `unzip`.
  77. #
  78. # depends echo_info
  79. echo_info "This function compliments the \`$1\` function."
  80. }
  81.  
  82.  
  83. ################################################################################
  84. # Filesystem operations
  85. ################################################################################
  86.  
  87. tar_dir() {
  88. # Tars dir as dir.tar. Removes dir.
  89. # compliments untar_dir
  90. tar -cf "$1.tar" "$(basename $1)" && rm -rf "$1"
  91. }
  92.  
  93. untar_dir() {
  94. # Untars dir.tar as dir. Removes dir.tar.
  95. # compliments tar_dir
  96. tar -xvf "$1" && rm "$1"
  97. }
  98.  
  99. targz_dir() {
  100. # Tars a dir as dir.tar.gz.
  101. # compliments untargz_dir
  102. tar -czf "$1.tar.gz" "$(basename $1)"; rm -rf "$1"
  103. }
  104.  
  105. untargz_dir() {
  106. # Untars dir.tar.gz as dir. Removes dir.tar.gz.
  107. # compliments targz_dir
  108. tar -xzvf "$1" && rm "$1"
  109. }
  110.  
  111. zip_dir() {
  112. # Zips dir as dir.zip. Removes dir.
  113. # compliments unzip_dir
  114. zip -r "$1.zip" "$(basename $1)" && rm -rf "$1"
  115. }
  116.  
  117. unzip_dir() {
  118. # Unzips dir.zip as dir. Removes dir.zip.
  119. # compliments zip_dir
  120. unzip "$1" && rm "$1"
  121. }
  122.  
  123. archive() {
  124. # Generic function for archiving directories
  125. #
  126. # depends tar_dir targz_dir zip_dir
  127. # compliments unarchive
  128. printf "Which type of archive would you like to like create? (tar, tar.gz, zip) "
  129. case $(read -e) in
  130. tar) tar_dir "$1";;
  131. tar.gz) targz_dir "$1";;
  132. zip) zip_dir "$1";;
  133. *) unsupported_input "tar, tar.gz, zip";;
  134. esac
  135. }
  136.  
  137. unarchive() {
  138. # Generic way to unarchive files.
  139. # Currently supports the following extensions:
  140. # - .tar
  141. # - .tar.gz
  142. # - .zip
  143. #
  144. # depends untar unzip
  145. # compliments archive
  146. case $1 in
  147. *.tar.gz) untargz_dir "$1";;
  148. *.tar) untar_dir "$1";;
  149. *.zip) unzip_dir "$1";;
  150. *) unsupported_input ".tar, .tar.zip, .zip"
  151. esac
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement