Advertisement
Guest User

registerHook.sh

a guest
Jan 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #! /bin/bash
  2. # (MIT, GPL3+) Alberto Salvia Novella (es20490446e)
  3.  
  4.  
  5. main () {
  6. checkPermissions
  7. insertHook "colors" "base"
  8. }
  9.  
  10.  
  11. checkPermissions () {
  12. user=$(id -u)
  13.  
  14. if [ "${user}" -ne 0 ]; then
  15. (>&2 echo "Not enough permissions")
  16. echo "Type 'sudo' or 'su;' before this command"
  17. exit 1
  18. fi
  19. }
  20.  
  21.  
  22. execute () {
  23. function="${1}"
  24. command="${2}"
  25. error=$(eval "${command}" 2>&1 >"/dev/null")
  26.  
  27. if [ ${?} -ne 0 ]; then
  28. echo "${function}: $error"
  29. exit 1
  30. fi
  31. }
  32.  
  33.  
  34. insert () {
  35. string="${1}"
  36. toInsert="${2}"
  37. separator="${3}"
  38.  
  39. if [ "${string}" != "" ]; then
  40. string=$(echo "${string}""${separator}")
  41. fi
  42.  
  43. echo "${string}""${toInsert}"
  44. }
  45.  
  46.  
  47. insertAfter () {
  48. string="${1}"
  49. toInsert="${2}"
  50. after="${3}"
  51. separator="${4}"
  52.  
  53. IFS="${separator}" read -r -a string <<< "$string"
  54.  
  55. for element in "${string[@]}"; do
  56. if [ "${lastElement}" = "${after}" ] && [ "${element}" != "${toInsert}" ]; then
  57. inserted=$(insert "${inserted}" "${toInsert}" "${separator}")
  58. fi
  59.  
  60. inserted=$(insert "${inserted}" "${element}" "${separator}")
  61. lastElement="${element}"
  62. done
  63.  
  64. echo "${inserted}"
  65. }
  66.  
  67.  
  68. insertHook () {
  69. toInsert=${1}
  70. after=${2}
  71.  
  72. file="/etc/mkinitcpio.conf"
  73. hooks=$(variableInFile "HOOKS" "${file}")
  74. inserted=$(insertAfter "${hooks}" "${toInsert}" "${after}" " ")
  75. modifyVariableInFile "HOOKS" "${inserted}" "${file}"
  76. updateInitImage
  77. }
  78.  
  79.  
  80. modifyVariableInFile () {
  81. variable="${1}"
  82. content="${2}"
  83. file="${3}"
  84.  
  85. if [ ! -f "${file}" ]; then
  86. echo "modifyVariableInFile: file doesn't exist: ${file}"
  87. exit 1
  88. fi
  89.  
  90. sed -i "s/^${variable}\=.*/${variable}=\"${content}\"/" "${file}"
  91. }
  92.  
  93.  
  94. updateInitImage () {
  95. execute "updateInitImage" "mkinitcpio"
  96. }
  97.  
  98.  
  99. variableInFile () {
  100. variable="${1}"
  101. file="${2}"
  102.  
  103. source "${file}"
  104. eval value=\$\{${variable}\}
  105. echo "${value}"
  106. }
  107.  
  108.  
  109. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement