Guest User

Untitled

a guest
Oct 17th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Stop on error
  4. set -e
  5.  
  6. helpfn() {
  7. echo -e "USAGE: $(basename $0) FILE... [OPTION]..." >&2
  8. echo -e "Replace environment variables in file(s)." >&2
  9. echo -e "" >&2
  10. echo -e "OPTIONS:" >&2
  11. echo -e " -e, --echo-only Don't modify file in place, output result to stdout." >&2
  12. echo -e " -h, --help Show this help." >&2
  13. echo -e " -o, --opening Specify opening character sequence (default: #{{env: )." >&2
  14. echo -e " -c, --closing Specify closing character sequence (default: }} )." >&2
  15. }
  16.  
  17. CRESET="\e[0m"
  18. CRED="\e[31m"
  19. CGREEN="\e[32m"
  20. CBLUE="\e[34m"
  21.  
  22. echo -ne "${CRED}"
  23. OPTS=`getopt -o ho:c:e --long help,opening:,closing:,debug,echo-only -n 'parse-options' -- "$@"`
  24. echo -ne "${CRESET}"
  25.  
  26. if [ $? != 0 ]; then
  27. echo -e "${CRED}Failed parsing options.${CRESET}" >&2
  28. helpfn
  29. exit 1
  30. fi
  31.  
  32. eval set -- "$OPTS"
  33.  
  34. OPENING='#{{env:'
  35. CLOSING='}}'
  36. HELP=false
  37. ECHO_ONLY=false
  38. DEBUG=false
  39.  
  40. while true; do
  41. case "$1" in
  42. -h | --help ) HELP=true; break ;;
  43. -o | --opening ) OPENING="$2"; shift 2 ;;
  44. -c | --closing ) CLOSING="$2"; shift 2 ;;
  45. -e | --echo-only ) ECHO_ONLY=true; shift ;;
  46. --debug ) DEBUG=true; shift ;;
  47. -- ) shift; break ;;
  48. * ) break ;;
  49. esac
  50. done
  51.  
  52. if $HELP; then
  53. helpfn
  54. exit 0
  55. fi
  56.  
  57. if [[ $# -lt 1 ]]; then
  58. echo -ne "${CRED}" >&2
  59. helpfn
  60. echo -ne "${CRESET}" >&2
  61. exit 1
  62. fi
  63.  
  64. #============================================================
  65.  
  66. if $DEBUG; then set -x; fi
  67.  
  68. substenv_rep() {
  69. FILE="$1"
  70.  
  71. # Find referenced variables
  72. vars=$(grep -oe "${OPENING}\([A-Za-z0-9_]*\)${CLOSING}" "$FILE" | \
  73. sort -u | \
  74. sed "s/${OPENING}\([A-Za-z0-9_]*\)${CLOSING}/\1/")
  75.  
  76. echo -e "${CBLUE}$(echo $vars | wc -w) variables found: $(echo $vars).${CRESET}" >&2
  77.  
  78. tmpfile="$(dirname $FILE)/.$(basename $FILE).substenv-ng~"
  79. cp "$FILE" "$tmpfile"
  80.  
  81. for var in $vars; do
  82. value=$(printenv $var | sed 's/\//\\\//g')
  83. sed -i "s/${OPENING}${var}${CLOSING}/${value}/g" "$tmpfile"
  84. done
  85.  
  86. cat "$tmpfile"
  87. rm "$tmpfile"
  88. }
  89.  
  90. while [[ $# -ge 1 ]]; do
  91. FILE="$1"
  92. orig="$(dirname $FILE)/.$(basename $FILE).orig"
  93.  
  94. if [[ ! -f "$orig" ]]; then
  95. if [[ ! -f "$FILE" ]]; then
  96. echo -e "${CRED}No file found: \"${FILE}\".${CRESET}" >&2
  97. exit 1
  98. fi
  99. cp "$FILE" "$orig"
  100. else
  101. echo -e "${CBLUE}Starting from \"$orig\".${CRESET}" >&2
  102. fi
  103.  
  104. if $ECHO_ONLY; then
  105. substenv_rep "$orig" $OPENING $CLOSING
  106. else
  107. substenv_rep "$orig" $OPENING $CLOSING > "$FILE"
  108. echo -ne "${CGREEN}Replaced environment variables in \"$FILE\". " >&2
  109. echo -e "${CBLUE}Original backed up to \"$orig\".${CRESET}" >&2
  110. fi
  111. shift
  112. done
Add Comment
Please, Sign In to add comment