Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #!/bin/bash
  2. source /home/myuser/test/config
  3. echo "Name=$nam" >&2
  4. echo "Surname=$sur" >&2
  5.  
  6. nam="Mark"
  7. sur="Brown"
  8.  
  9. password=bar
  10. echo rm -rf /
  11. PROMPT_COMMAND='echo "Sending your last command $(history 1) to my email"'
  12. hostname=localhost; echo rm -rf /
  13.  
  14. #!/bin/bash
  15. typeset -A config # init array
  16. config=( # set default values in config array
  17. [username]="root"
  18. [password]=""
  19. [hostname]="localhost"
  20. )
  21.  
  22. while read line
  23. do
  24. if echo $line | grep -F = &>/dev/null
  25. then
  26. varname=$(echo "$line" | cut -d '=' -f 1)
  27. config[$varname]=$(echo "$line" | cut -d '=' -f 2-)
  28. fi
  29. done < myscript.conf
  30.  
  31. echo ${config[username]} # should be loaded from defaults
  32. echo ${config[password]} # should be loaded from config file
  33. echo ${config[hostname]} # includes the "injected" code, but it's fine here
  34. echo ${config[PROMPT_COMMAND]} # also respects variables that you may not have
  35. # been looking for, but they're sandboxed inside the $config array
  36.  
  37. #!/bin/bash
  38. config() {
  39. val=$(grep -E "^$1=" myscript.conf 2>/dev/null || echo "$1=__DEFAULT__" | head -n 1 | cut -d '=' -f 2-)
  40.  
  41. if [[ $val == __DEFAULT__ ]]
  42. then
  43. case $1 in
  44. username)
  45. echo -n "root"
  46. ;;
  47. password)
  48. echo -n ""
  49. ;;
  50. hostname)
  51. echo -n "localhost"
  52. ;;
  53. esac
  54. else
  55. echo -n $val
  56. fi
  57. }
  58.  
  59. echo $(config username) # should be loaded from defaults
  60. echo $(config password) # should be loaded from config file
  61. echo $(config hostname) # includes the "injected" code, but it's fine here
  62. echo $(config PROMPT_COMMAND) # also respects variables that you may not have
  63. # been looking for, but they're sandboxed inside the $config array
  64.  
  65. myvar=Hello World
  66.  
  67. myvar=Default Value
  68. othervar=Another Variable
  69.  
  70. config_read_file() {
  71. (grep -E "^${2}=" -m 1 "${1}" 2>/dev/null || echo "VAR=__UNDEFINED__") | head -n 1 | cut -d '=' -f 2-;
  72. }
  73.  
  74. config_get() {
  75. val="$(config_read_file config.cfg "${1}")";
  76. if [ "${val}" = "__UNDEFINED__" ]; then
  77. val="$(config_read_file config.cfg.defaults "${1}")";
  78. fi
  79. printf -- "%s" "${val}";
  80. }
  81.  
  82. #!/usr/bin/env bash
  83. source config.shlib; # load the config library functions
  84. echo "$(config_get myvar)"; # will be found in user-cfg
  85. printf -- "%sn" "$(config_get myvar)"; # safer way of echoing!
  86. myvar="$(config_get myvar)"; # how to just read a value without echoing
  87. echo "$(config_get othervar)"; # will fall back to defaults
  88. echo "$(config_get bleh)"; # "__UNDEFINED__" since it isn't set anywhere
  89.  
  90. source /home/myuser/test/config
  91.  
  92. . /home/myuser/test/config
  93.  
  94. <config>
  95. <username>username-or-email</username>
  96. <password>the-password</password>
  97. </config>
  98.  
  99. username="$( xml sel -t -v '/config/username' "$config_file" )"
  100.  
  101. {
  102. "username": "username-or-email",
  103. "password": "the-password"
  104. }
  105.  
  106. username="$( jq -r '.username' "$config_file" )"
  107.  
  108. CONF=${XDG_CONFIG_HOME:-~/config}/myscript.sh
  109. if [ ! -f $CONF ]; then
  110. cat > $CONF << CONF
  111. VAR1="default value"
  112. CONF
  113. fi
  114. . <(sed 's/^([^=]+) *= *(.*)$/1=${1:-2}/' < $CONF)
  115.  
  116. #!/bin/bash
  117. name="mohsen"
  118. age=35
  119. cat > /home/myuser/test/config << EOF
  120. Name=$name
  121. Age=$age
  122. EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement