Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

206 wiersze
5.1 KiB

  1. #!/usr/bin/env bash
  2. ### BEG SCRIPT INFO
  3. #
  4. # Header:
  5. #
  6. # fname : "awesome-ninja-admins"
  7. # cdate : "24.05.2018"
  8. # author : "Michał Żurawski <trimstray@gmail.com>"
  9. # tab_size : "2"
  10. # soft_tabs : "yes"
  11. #
  12. # Description:
  13. #
  14. # See README.md file for more information.
  15. #
  16. # License:
  17. #
  18. # awesome-ninja-admins, Copyright (C) 2018 Michał Żurawski
  19. #
  20. # This program is free software: you can redistribute it and/or modify
  21. # it under the terms of the GNU General Public License as published by
  22. # the Free Software Foundation, either version 3 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU General Public License
  31. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. #
  33. ### END SCRIPT INFO
  34. # The array that store call parameters.
  35. # shellcheck disable=SC2034
  36. __init_params=()
  37. __script_params=("$@")
  38. # Store the name of the script and directory call.
  39. readonly _init_name="$(basename "$0")"
  40. readonly _init_directory="$(dirname "$(readlink -f "$0")")"
  41. # Set root directory.
  42. readonly _rel="${_init_directory}"
  43. # Directory structure.
  44. # shellcheck disable=SC2154
  45. readonly _src="${_rel}/src"
  46. readonly _lib="${_rel}/lib"
  47. readonly _cfg="${_rel}/etc"
  48. # shellcheck disable=SC1090,SC1091
  49. source "${_src}/settings"
  50. # shellcheck disable=SC1090
  51. source "${_src}/helpers"
  52. ################################################################################
  53. ######################### Main function (script init) ##########################
  54. ################################################################################
  55. function __main__() {
  56. local _FUNCTION_ID="__main__"
  57. local _STATE="0"
  58. # Stores the current date.
  59. readonly _cdate=$(date +%Y%m%d)
  60. # External configuration file (-c|--config script param).
  61. config=""
  62. load_state="0"
  63. # Declaration of output variables (--debug and --verbose params).
  64. stdout_mode=""
  65. printf_mode=""
  66. # Enable/disable output colors.
  67. # shellcheck disable=SC2034
  68. s_color="true"
  69. # We place here used commands at script runtime, as strings to anything
  70. # unnecessarily run.
  71. readonly commands=("basename" "dirname" "stat" "date" "grep" "egrep" "cut" \
  72. "sed" "gzip" "tar")
  73. # If you intend to specify the full path to the command we do it like:
  74. # readonly exec_gzip="/bin/gzip"
  75. # Stores the names of the missing commands.
  76. missing_hash=()
  77. missing_counter="0"
  78. for i in "${commands[@]}" ; do
  79. if [[ ! -z "$i" ]] ; then
  80. hash "$i" >/dev/null 2>&1 ; state="$?"
  81. # If the command was not found put it in the array
  82. if [[ "$state" -ne 0 ]] ; then
  83. missing_hash+=("$i")
  84. ((missing_counter++))
  85. fi
  86. fi
  87. done
  88. # It is a good idea to terminate the script at this stage
  89. # with information for the user to fix the errors if at least one
  90. # of the required commands in the commands array is not found.
  91. if [[ "$missing_counter" -gt 0 ]] ; then
  92. printf "not found in PATH: %s\\n" "${missing_hash[*]}" >&2
  93. exit 1
  94. fi
  95. # Specifies the call parameters of the script, the exact description
  96. # can be found in _help_ and file README.md.
  97. local _short_opt=""
  98. local _long_opt="help"
  99. _GETOPT_PARAMS=$(getopt -o "${_short_opt}" --long "${_long_opt}" \
  100. -n "${_init_name}" -- "${__script_params[@]}")
  101. # With this structure, in the case of problems with the parameters placed
  102. # in the _GETOPT_PARAMS variable we finish the script. Keep this in mind
  103. # because it has some consequences - the __main __() function will not be
  104. # executed.
  105. # Ends an error if the parameter or its argument is not valid.
  106. _getopt_state="$?"
  107. if [ "$_getopt_state" != 0 ] ; then
  108. exit 1
  109. fi
  110. eval set -- "$_GETOPT_PARAMS"
  111. while true ; do
  112. case $1 in
  113. --help)
  114. _help_
  115. shift ; exit 0 ;;
  116. *)
  117. if [[ "$2" == "-" ]] || [[ ! -z "$2" ]] ; then
  118. printf "%s: invalid option -- '%s'\\n" "$_init_name" "$2"
  119. exit 1
  120. # elif [[ -z "$2" ]] ; then break ; fi
  121. else break ; fi
  122. ;;
  123. esac
  124. done
  125. ################################# USER SPACE #################################
  126. # ````````````````````````````````````````````````````````````````````````````
  127. # Put here all your variable declarations, function calls
  128. # and all the other code blocks.
  129. # In this section we add external file (for -c|--config script param).
  130. if [[ "$load_state" -eq 1 ]] ; then _load "head" "$config" ; fi
  131. # shellcheck disable=SC2034
  132. # Generate random value.
  133. _random=$(date +"%s")
  134. local _user
  135. _user=$(whoami)
  136. _sprintf "head" "Awesome-Ninja-Admins"
  137. _sprintf "info" "tasks:"
  138. _init_skel
  139. echo
  140. # ````````````````````````````````````````````````````````````````````````````
  141. return "$_STATE"
  142. }
  143. # We pass arguments to the __main__ function.
  144. # It is required if you want to run on arguments type $1, $2, ...
  145. __main__ "${__script_params[@]}"
  146. exit 0