├── .travis.yml ├── LICENSE ├── README ├── TODO ├── read_ini.sh └── test ├── test.sh ├── test1.ini ├── test1.out.correct ├── test1.sh ├── test2.ini ├── test2.out.correct ├── test2.sh ├── test3.ini ├── test3.out.correct ├── test3.sh ├── test4.ini ├── test4.out.correct ├── test4.sh ├── test5.ini ├── test5.out.correct ├── test5.sh ├── test6.ini ├── test6.out.correct ├── test6.sh ├── test7.ini ├── test7.out.correct ├── test7.sh ├── test8.ini ├── test8.out.correct ├── test8.sh ├── test9.ini ├── test9.out.correct └── test9.sh /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | 3 | compiler: 4 | - gcc 5 | 6 | before_install: 7 | 8 | script: cd test && ./test.sh 9 | 10 | ## whitelist 11 | branches: 12 | only: 13 | - master 14 | - next 15 | 16 | notifications: 17 | email: 18 | - sweet_f_a@gmx.de 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This software is provided under the BSD license. The text of this license 2 | is provided below: 3 | 4 | -------------------------------------------------------------------------- 5 | 6 | Copyright (C) 2009 Kevin Porter / Advanced Web Construction Ltd 7 | Copyright (C) 2010-2014 Ruediger Meier 8 | All rights reserved. 9 | 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions 12 | are met: 13 | 14 | 1. Redistributions of source code must retain the above copyright 15 | notice, this list of conditions and the following disclaimer. 16 | 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 21 | 3. Neither the name of the author nor the names of any contributors 22 | may be used to endorse or promote products derived from this 23 | software without specific prior written permission. 24 | 25 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR 26 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 27 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 28 | DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 32 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 34 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 35 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | 2 | bash_ini_parser -- Simple INI file parser 3 | ========================================= 4 | 5 | This is a comfortable and simple INI file parser to be used in 6 | bash scripts. 7 | 8 | 9 | 10 | 11 | COPYRIGHT 12 | --------- 13 | 14 | Copyright (c) 2009 Kevin Porter / Advanced Web Construction Ltd 15 | (http://coding.tinternet.info / http://webutils.co.uk) 16 | Copyright (c) 2010-2014 Ruediger Meier 17 | (https://github.com/rudimeier/) 18 | 19 | License: BSD-3-Clause, see LICENSE file 20 | 21 | 22 | 23 | 24 | USAGE 25 | ----- 26 | 27 | You must source the bash file into your script: 28 | 29 | > . read_ini.sh 30 | 31 | and then use the read_ini function, defined as: 32 | 33 | > read_ini INI_FILE [SECTION] [[--prefix|-p] PREFIX] [[--booleans|b] [0|1]] 34 | 35 | If SECTION is supplied, then only the specified section of the file will 36 | be processed. 37 | 38 | After running the read_ini function, variables corresponding to the ini 39 | file entries will be available to you. Naming convention for variable 40 | names is: 41 | 42 | PREFIX__SECTION__VARNAME 43 | 44 | PREFIX is 'INI' by default (but can be changed with the --prefix option), 45 | SECTION and VARNAME are the section name and variable name respectively. 46 | 47 | Additionally you can get a list of all these variable names: 48 | PREFIX__ALL_VARS 49 | and get a list of sections: 50 | PREFIX__ALL_SECTIONS 51 | and the number of sections: 52 | PREFIX__NUMSECTIONS 53 | 54 | For example, to read and output the variables of this ini file: 55 | 56 | -- START test1.ini file 57 | 58 | var1="VAR 1" 59 | var2 = VAR 2 60 | 61 | [section1] 62 | var1="section1 VAR 1" 63 | var2= section1 VAR 2 64 | 65 | 66 | -- END test1.ini file 67 | 68 | you could do this: 69 | 70 | -- START bash script 71 | 72 | . read_ini.sh 73 | 74 | read_ini test1.ini 75 | 76 | echo "var1 = ${INI__var1}" 77 | echo "var2 = ${INI__var2}" 78 | echo "section1 var1 = ${INI__section1__var1}" 79 | echo "section1 var2 = ${INI__section1__var2}" 80 | 81 | echo "list of all ini vars: ${INI__ALL_VARS}" 82 | echo "number of sections: ${INI__NUMSECTIONS}" 83 | 84 | -- END bash script 85 | 86 | 87 | 88 | 89 | OPTIONS 90 | ------- 91 | 92 | [--prefix | -p] PREFIX 93 | String to prepend to generated variable names (automatically followed by '__'). 94 | Default: INI 95 | 96 | [--booleans | -b] [0|1] 97 | Whether to interpret special unquoted string values 'yes', 'no', 'true', 98 | 'false', 'on', 'off' as booleans. 99 | Default: 1 100 | 101 | 102 | 103 | 104 | INI FILE FORMAT 105 | --------------- 106 | 107 | - Variables are stored as name/value pairs, eg: 108 | var=value 109 | 110 | - Leading and trailing whitespace of the name and the value is discarded. 111 | 112 | - Use double or single quotes to get whitespace in the values 113 | 114 | - Section names in square brackets, eg: 115 | [section1] 116 | var1 = value 117 | 118 | - Variable names can be re-used between sections (or out of section), eg: 119 | var1=value 120 | [section1] 121 | var1=value 122 | [section3] 123 | var1=value 124 | 125 | - Dots are converted to underscores in all variable names. 126 | 127 | - Special boolean values: unquoted strings 'yes', 'true' and 'on' are interpreted 128 | as 1; 'no', 'false' and 'off' are interpreted as 0 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | 2 | 3 | - Tabs/newlines to be preserved 4 | 5 | - [] notation for arrays (like PHP's parse_ini_file()) 6 | 7 | 8 | -------------------------------------------------------------------------------- /read_ini.sh: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2009 Kevin Porter / Advanced Web Construction Ltd 3 | # (http://coding.tinternet.info, http://webutils.co.uk) 4 | # Copyright (c) 2010-2014 Ruediger Meier 5 | # (https://github.com/rudimeier/) 6 | # 7 | # License: BSD-3-Clause, see LICENSE file 8 | # 9 | # Simple INI file parser. 10 | # 11 | # See README for usage. 12 | # 13 | # 14 | 15 | 16 | 17 | 18 | function read_ini() 19 | { 20 | # Be strict with the prefix, since it's going to be run through eval 21 | function check_prefix() 22 | { 23 | if ! [[ "${VARNAME_PREFIX}" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]] ;then 24 | echo "read_ini: invalid prefix '${VARNAME_PREFIX}'" >&2 25 | return 1 26 | fi 27 | } 28 | 29 | function check_ini_file() 30 | { 31 | if [ ! -r "$INI_FILE" ] ;then 32 | echo "read_ini: '${INI_FILE}' doesn't exist or not" \ 33 | "readable" >&2 34 | return 1 35 | fi 36 | } 37 | 38 | # enable some optional shell behavior (shopt) 39 | function pollute_bash() 40 | { 41 | if ! shopt -q extglob ;then 42 | SWITCH_SHOPT="${SWITCH_SHOPT} extglob" 43 | fi 44 | if ! shopt -q nocasematch ;then 45 | SWITCH_SHOPT="${SWITCH_SHOPT} nocasematch" 46 | fi 47 | shopt -q -s ${SWITCH_SHOPT} 48 | } 49 | 50 | # unset all local functions and restore shopt settings before returning 51 | # from read_ini() 52 | function cleanup_bash() 53 | { 54 | shopt -q -u ${SWITCH_SHOPT} 55 | unset -f check_prefix check_ini_file pollute_bash cleanup_bash 56 | } 57 | 58 | local INI_FILE="" 59 | local INI_SECTION="" 60 | 61 | # {{{ START Deal with command line args 62 | 63 | # Set defaults 64 | local BOOLEANS=1 65 | local VARNAME_PREFIX=INI 66 | local CLEAN_ENV=0 67 | 68 | # {{{ START Options 69 | 70 | # Available options: 71 | # --boolean Whether to recognise special boolean values: ie for 'yes', 'true' 72 | # and 'on' return 1; for 'no', 'false' and 'off' return 0. Quoted 73 | # values will be left as strings 74 | # Default: on 75 | # 76 | # --prefix=STRING String to begin all returned variables with (followed by '__'). 77 | # Default: INI 78 | # 79 | # First non-option arg is filename, second is section name 80 | 81 | while [ $# -gt 0 ] 82 | do 83 | 84 | case $1 in 85 | 86 | --clean | -c ) 87 | CLEAN_ENV=1 88 | ;; 89 | 90 | --booleans | -b ) 91 | shift 92 | BOOLEANS=$1 93 | ;; 94 | 95 | --prefix | -p ) 96 | shift 97 | VARNAME_PREFIX=$1 98 | ;; 99 | 100 | * ) 101 | if [ -z "$INI_FILE" ] 102 | then 103 | INI_FILE=$1 104 | else 105 | if [ -z "$INI_SECTION" ] 106 | then 107 | INI_SECTION=$1 108 | fi 109 | fi 110 | ;; 111 | 112 | esac 113 | 114 | shift 115 | done 116 | 117 | if [ -z "$INI_FILE" ] && [ "${CLEAN_ENV}" = 0 ] ;then 118 | echo -e "Usage: read_ini [-c] [-b 0| -b 1]] [-p PREFIX] FILE"\ 119 | "[SECTION]\n or read_ini -c [-p PREFIX]" >&2 120 | cleanup_bash 121 | return 1 122 | fi 123 | 124 | if ! check_prefix ;then 125 | cleanup_bash 126 | return 1 127 | fi 128 | 129 | local INI_ALL_VARNAME="${VARNAME_PREFIX}__ALL_VARS" 130 | local INI_ALL_SECTION="${VARNAME_PREFIX}__ALL_SECTIONS" 131 | local INI_NUMSECTIONS_VARNAME="${VARNAME_PREFIX}__NUMSECTIONS" 132 | if [ "${CLEAN_ENV}" = 1 ] ;then 133 | eval unset "\$${INI_ALL_VARNAME}" 134 | fi 135 | unset ${INI_ALL_VARNAME} 136 | unset ${INI_ALL_SECTION} 137 | unset ${INI_NUMSECTIONS_VARNAME} 138 | 139 | if [ -z "$INI_FILE" ] ;then 140 | cleanup_bash 141 | return 0 142 | fi 143 | 144 | if ! check_ini_file ;then 145 | cleanup_bash 146 | return 1 147 | fi 148 | 149 | # Sanitise BOOLEANS - interpret "0" as 0, anything else as 1 150 | if [ "$BOOLEANS" != "0" ] 151 | then 152 | BOOLEANS=1 153 | fi 154 | 155 | 156 | # }}} END Options 157 | 158 | # }}} END Deal with command line args 159 | 160 | local LINE_NUM=0 161 | local SECTIONS_NUM=0 162 | local SECTION="" 163 | 164 | # IFS is used in "read" and we want to switch it within the loop 165 | local IFS=$' \t\n' 166 | local IFS_OLD="${IFS}" 167 | 168 | # we need some optional shell behavior (shopt) but want to restore 169 | # current settings before returning 170 | local SWITCH_SHOPT="" 171 | pollute_bash 172 | 173 | while read -r line || [ -n "$line" ] 174 | do 175 | #echo line = "$line" 176 | 177 | ((LINE_NUM++)) 178 | 179 | # Skip blank lines and comments 180 | if [ -z "$line" -o "${line:0:1}" = ";" -o "${line:0:1}" = "#" ] 181 | then 182 | continue 183 | fi 184 | 185 | # Section marker? 186 | if [[ "${line}" =~ ^\[[a-zA-Z0-9_]{1,}\]$ ]] 187 | then 188 | 189 | # Set SECTION var to name of section (strip [ and ] from section marker) 190 | SECTION="${line#[}" 191 | SECTION="${SECTION%]}" 192 | eval "${INI_ALL_SECTION}=\"\${${INI_ALL_SECTION}# } $SECTION\"" 193 | ((SECTIONS_NUM++)) 194 | 195 | continue 196 | fi 197 | 198 | # Are we getting only a specific section? And are we currently in it? 199 | if [ ! -z "$INI_SECTION" ] 200 | then 201 | if [ "$SECTION" != "$INI_SECTION" ] 202 | then 203 | continue 204 | fi 205 | fi 206 | 207 | # Valid var/value line? (check for variable name and then '=') 208 | if ! [[ "${line}" =~ ^[a-zA-Z0-9._]{1,}[[:space:]]*= ]] 209 | then 210 | echo "Error: Invalid line:" >&2 211 | echo " ${LINE_NUM}: $line" >&2 212 | cleanup_bash 213 | return 1 214 | fi 215 | 216 | 217 | # split line at "=" sign 218 | IFS="=" 219 | read -r VAR VAL <<< "${line}" 220 | IFS="${IFS_OLD}" 221 | 222 | # delete spaces around the equal sign (using extglob) 223 | VAR="${VAR%%+([[:space:]])}" 224 | VAL="${VAL##+([[:space:]])}" 225 | VAR=$(echo $VAR) 226 | 227 | 228 | # Construct variable name: 229 | # ${VARNAME_PREFIX}__$SECTION__$VAR 230 | # Or if not in a section: 231 | # ${VARNAME_PREFIX}__$VAR 232 | # In both cases, full stops ('.') are replaced with underscores ('_') 233 | if [ -z "$SECTION" ] 234 | then 235 | VARNAME=${VARNAME_PREFIX}__${VAR//./_} 236 | else 237 | VARNAME=${VARNAME_PREFIX}__${SECTION}__${VAR//./_} 238 | fi 239 | eval "${INI_ALL_VARNAME}=\"\${${INI_ALL_VARNAME}# } ${VARNAME}\"" 240 | 241 | if [[ "${VAL}" =~ ^\".*\"$ ]] 242 | then 243 | # remove existing double quotes 244 | VAL="${VAL##\"}" 245 | VAL="${VAL%%\"}" 246 | elif [[ "${VAL}" =~ ^\'.*\'$ ]] 247 | then 248 | # remove existing single quotes 249 | VAL="${VAL##\'}" 250 | VAL="${VAL%%\'}" 251 | elif [ "$BOOLEANS" = 1 ] 252 | then 253 | # Value is not enclosed in quotes 254 | # Booleans processing is switched on, check for special boolean 255 | # values and convert 256 | 257 | # here we compare case insensitive because 258 | # "shopt nocasematch" 259 | case "$VAL" in 260 | yes | true | on ) 261 | VAL=1 262 | ;; 263 | no | false | off ) 264 | VAL=0 265 | ;; 266 | esac 267 | fi 268 | 269 | 270 | # enclose the value in single quotes and escape any 271 | # single quotes and backslashes that may be in the value 272 | VAL="${VAL//\\/\\\\}" 273 | VAL="\$'${VAL//\'/\'}'" 274 | 275 | eval "$VARNAME=$VAL" 276 | done <"${INI_FILE}" 277 | 278 | # return also the number of parsed sections 279 | eval "$INI_NUMSECTIONS_VARNAME=$SECTIONS_NUM" 280 | 281 | cleanup_bash 282 | } 283 | 284 | 285 | -------------------------------------------------------------------------------- /test/test.sh: -------------------------------------------------------------------------------- 1 | 2 | DIR=$(dirname $0) 3 | cd $DIR 4 | 5 | TESTS=$(ls test*.sh | grep -v test.sh | sed 's/\.sh$//') 6 | 7 | for test in $TESTS 8 | do 9 | 10 | bash $test.sh &> $test.out 11 | # bash $test.sh >$test.out 2>$test.err 12 | 13 | # Fail and bail out if test didn't pass 14 | PASSED=$(diff $test.out $test.out.correct 2>&1) 15 | if [ ! -z "$PASSED" ] 16 | then 17 | echo "Test $test failed. Output is in $DIR/$test.out" 18 | exit 1 19 | else 20 | rm -rf $test.out 21 | fi 22 | 23 | done 24 | 25 | 26 | echo "All tests passed" 27 | 28 | 29 | -------------------------------------------------------------------------------- /test/test1.ini: -------------------------------------------------------------------------------- 1 | 2 | ; Comment beginning with ';' 3 | # Comment beginning with '#' 4 | 5 | ; var1 - simple var 6 | var1=VAR 1 7 | 8 | ; var2 - space before '='; leading space that should be stripped 9 | var2 = VAR 2 10 | 11 | ; var3 - with leading and trailing space that should be stripped 12 | var3 = VAR 3 13 | 14 | ; var4 - value in double quotes 15 | var4="VAR 4" 16 | 17 | ; var5 - value in double quotes; value's leading and trailing whitespace should 18 | ; be preserved; leading and trailing whitespace before/after double quotes should 19 | ; not become part of the value 20 | var5 = " VAR 5 " 21 | 22 | ; var6 - value in double quotes; value's leading and trailing whitespace should 23 | ; be preserved; leading and trailing whitespace before/after double quotes should 24 | ; not become part of the value 25 | var6 = ' VAR 6 ' 26 | 27 | ; var7 - value contains a single double quote 28 | var7 = VAR " 7 29 | 30 | ; var8 - value contains a single single quote 31 | var8 = VAR ' 8 32 | 33 | ; var9 - leading whitespace before variable name 34 | var9=VAR 9 35 | 36 | ; var10 - leading whitespace before variable name 37 | var10 = VAR 10 38 | 39 | ; var11 - no newline at the end of file 40 | var11 = VAR 11 -------------------------------------------------------------------------------- /test/test1.out.correct: -------------------------------------------------------------------------------- 1 | var1:VAR 1 2 | var2:VAR 2 3 | var3:VAR 3 4 | var4:VAR 4 5 | var5: VAR 5 6 | var6: VAR 6 7 | var7:VAR " 7 8 | var8:VAR ' 8 9 | var9:VAR 9 10 | var10:VAR 10 11 | var11:VAR 11 12 | -------------------------------------------------------------------------------- /test/test1.sh: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Test 1 4 | # 5 | # Basic variable values; leading/trailing whitespace; comments 6 | 7 | . ../read_ini.sh 8 | read_ini test1.ini 9 | 10 | #echo "a:LOCALVAR=$LOCALVAR" 11 | echo "var1:$INI__var1" 12 | echo "var2:$INI__var2" 13 | echo "var3:$INI__var3" 14 | echo "var4:$INI__var4" 15 | echo "var5:$INI__var5" 16 | echo "var6:$INI__var6" 17 | echo "var7:$INI__var7" 18 | echo "var8:$INI__var8" 19 | echo "var9:$INI__var9" 20 | echo "var10:$INI__var10" 21 | echo "var11:$INI__var11" 22 | -------------------------------------------------------------------------------- /test/test2.ini: -------------------------------------------------------------------------------- 1 | 2 | 3 | var1=VAR1 4 | 5 | ; Invalid line - should throw an error and stop processing 6 | INVALID LINE 7 | var2=VAR2 8 | 9 | -------------------------------------------------------------------------------- /test/test2.out.correct: -------------------------------------------------------------------------------- 1 | Error: Invalid line: 2 | 6: INVALID LINE 3 | -------------------------------------------------------------------------------- /test/test2.sh: -------------------------------------------------------------------------------- 1 | 2 | # Test 2 3 | # 4 | # Invalid line 5 | 6 | . ../read_ini.sh 7 | read_ini test2.ini 8 | 9 | -------------------------------------------------------------------------------- /test/test3.ini: -------------------------------------------------------------------------------- 1 | 2 | ; Testing sections 3 | 4 | var1=VAR 1 5 | var2= "VAR 2 " 6 | 7 | [section1] 8 | var1="section 1 VAR 1" 9 | var2= "section 1 VAR 2" 10 | var3 = "section 1 VAR 3 " 11 | 12 | [section2] 13 | var1 = section 2 VAR 1 14 | var2=" section 2 VAR 2" 15 | var3=section 2 VAR 3 16 | var4=section 2 VAR 4 17 | var5=section 2 VAR 5 18 | 19 | [section3] 20 | var1="section 3 VAR 1" 21 | var2= "section 3 VAR 2" 22 | 23 | 24 | -------------------------------------------------------------------------------- /test/test3.out.correct: -------------------------------------------------------------------------------- 1 | var1:VAR 1 2 | var2:VAR 2 3 | setion1_var1:section 1 VAR 1 4 | setion1_var2:section 1 VAR 2 5 | setion1_var3:section 1 VAR 3 6 | setion2_var1:section 2 VAR 1 7 | setion2_var2: section 2 VAR 2 8 | setion2_var3:section 2 VAR 3 9 | setion2_var4:section 2 VAR 4 10 | setion2_var5:section 2 VAR 5 11 | setion3_var1:section 3 VAR 1 12 | setion3_var2:section 3 VAR 2 13 | number sections:3 14 | all sections:section1 section2 section3 15 | all variables:INI__var1 INI__var2 INI__section1__var1 INI__section1__var2 INI__section1__var3 INI__section2__var1 INI__section2__var2 INI__section2__var3 INI__section2__var4 INI__section2__var5 INI__section3__var1 INI__section3__var2 16 | -------------------------------------------------------------------------------- /test/test3.sh: -------------------------------------------------------------------------------- 1 | 2 | # Test 3 3 | # 4 | # Sections 5 | 6 | . ../read_ini.sh 7 | read_ini test3.ini 8 | 9 | echo "var1:$INI__var1" 10 | echo "var2:$INI__var2" 11 | 12 | echo "setion1_var1:$INI__section1__var1" 13 | echo "setion1_var2:$INI__section1__var2" 14 | echo "setion1_var3:$INI__section1__var3" 15 | 16 | echo "setion2_var1:$INI__section2__var1" 17 | echo "setion2_var2:$INI__section2__var2" 18 | echo "setion2_var3:$INI__section2__var3" 19 | echo "setion2_var4:$INI__section2__var4" 20 | echo "setion2_var5:$INI__section2__var5" 21 | 22 | echo "setion3_var1:$INI__section3__var1" 23 | echo "setion3_var2:$INI__section3__var2" 24 | 25 | echo "number sections:$INI__NUMSECTIONS" 26 | echo "all sections:$INI__ALL_SECTIONS" 27 | echo "all variables:$INI__ALL_VARS" 28 | -------------------------------------------------------------------------------- /test/test4.ini: -------------------------------------------------------------------------------- 1 | 2 | ; Testing dots in variable names (these get converted to underscores in 3 | ; output variable names) 4 | 5 | var.1 = "VAR 1" 6 | var.two="VAR 2" 7 | var.3.two_dots="VAR 3" 8 | 9 | 10 | [section1] 11 | var.1="section 1 VAR 1" 12 | var.two="section 1 VAR 2" 13 | 14 | 15 | -------------------------------------------------------------------------------- /test/test4.out.correct: -------------------------------------------------------------------------------- 1 | var_1:VAR 1 2 | var_two:VAR 2 3 | var_3_two_dots:VAR 3 4 | section 1 var_1:section 1 VAR 1 5 | section 1 var_two:section 1 VAR 2 6 | -------------------------------------------------------------------------------- /test/test4.sh: -------------------------------------------------------------------------------- 1 | 2 | # Test 4 3 | # 4 | # Dots in variable names (converted to underscores in output var names) 5 | 6 | . ../read_ini.sh 7 | read_ini test4.ini 8 | 9 | echo "var_1:$INI__var_1" 10 | echo "var_two:$INI__var_two" 11 | echo "var_3_two_dots:$INI__var_3_two_dots" 12 | 13 | echo "section 1 var_1:$INI__section1__var_1" 14 | echo "section 1 var_two:$INI__section1__var_two" 15 | 16 | -------------------------------------------------------------------------------- /test/test5.ini: -------------------------------------------------------------------------------- 1 | 2 | ; Testing prevention of in-value code execution via eval 3 | var1 = blah blah `ls` blah 4 | var2 = blah blah $(ls) blah 5 | 6 | ; these code injections worked on 0.3 7 | var3 = blah blah \\`ls\\` blah 8 | var4 = blah blah \\$(ls) blah 9 | 10 | ; these code injections could work with read -r 11 | var5 = blah blah \`ls\` blah 12 | var6 = blah blah \$(ls) blah 13 | 14 | -------------------------------------------------------------------------------- /test/test5.out.correct: -------------------------------------------------------------------------------- 1 | var1:blah blah `ls` blah 2 | var2:blah blah $(ls) blah 3 | var3:blah blah \\`ls\\` blah 4 | var4:blah blah \\$(ls) blah 5 | var5:blah blah \`ls\` blah 6 | var6:blah blah \$(ls) blah 7 | -------------------------------------------------------------------------------- /test/test5.sh: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Test 5 4 | # 5 | # Prevention of in-value code execution via backticks or $() notation. 6 | # This must be done because the value is run through an eval statement. 7 | 8 | . ../read_ini.sh 9 | read_ini test5.ini 10 | 11 | echo "var1:$INI__var1" 12 | echo "var2:$INI__var2" 13 | echo "var3:$INI__var3" 14 | echo "var4:$INI__var4" 15 | echo "var5:$INI__var5" 16 | echo "var6:$INI__var6" 17 | -------------------------------------------------------------------------------- /test/test6.ini: -------------------------------------------------------------------------------- 1 | 2 | 3 | ; Testing processing of special boolean constants: unquoted 4 | ; strings 'yes', 'true' and 'on' are interpreted as 1; 5 | ; unquoted strings 'no', 'false' and 'off' as 0. 6 | 7 | yes1 = yes 8 | yes2 = "yes" 9 | yes3 = Yes 10 | true1=true 11 | true2= true 12 | true3 = "tRuE" 13 | on1 = on 14 | on2 = oN 15 | on3 = "ON" 16 | 17 | no1 = no 18 | no2 = "no" 19 | no3=No 20 | false1 = false 21 | false2 = faLSe 22 | false3 = "FALSE" 23 | off1 = off 24 | off2 = Off 25 | off3 = "off" 26 | 27 | 28 | -------------------------------------------------------------------------------- /test/test6.out.correct: -------------------------------------------------------------------------------- 1 | yes1:1 2 | yes2:yes 3 | yes3:1 4 | true1:1 5 | true2:1 6 | true3:tRuE 7 | on1:1 8 | on2:1 9 | on3:ON 10 | no1:0 11 | no2:no 12 | no3:0 13 | false1:0 14 | false2:0 15 | false3:FALSE 16 | off1:0 17 | off2:0 18 | off3:off 19 | yes1:1 20 | true2:1 21 | on3:ON 22 | no3:0 23 | false2:0 24 | off1:0 25 | yes1:yes 26 | true2:true 27 | on3:ON 28 | no3:No 29 | false2:faLSe 30 | off1:off 31 | -------------------------------------------------------------------------------- /test/test6.sh: -------------------------------------------------------------------------------- 1 | 2 | # Testing booleans processing 3 | 4 | # First: with default booleans processing (on) 5 | . ../read_ini.sh 6 | read_ini test6.ini 7 | 8 | echo "yes1:$INI__yes1" 9 | echo "yes2:$INI__yes2" 10 | echo "yes3:$INI__yes3" 11 | echo "true1:$INI__true1" 12 | echo "true2:$INI__true2" 13 | echo "true3:$INI__true3" 14 | echo "on1:$INI__on1" 15 | echo "on2:$INI__on2" 16 | echo "on3:$INI__on3" 17 | 18 | echo "no1:$INI__no1" 19 | echo "no2:$INI__no2" 20 | echo "no3:$INI__no3" 21 | echo "false1:$INI__false1" 22 | echo "false2:$INI__false2" 23 | echo "false3:$INI__false3" 24 | echo "off1:$INI__off1" 25 | echo "off2:$INI__off2" 26 | echo "off3:$INI__off3" 27 | 28 | 29 | # Second: with booleans processing explicitly turned on via "--booleans 1" 30 | . ../read_ini.sh 31 | read_ini --booleans 1 test6.ini 32 | 33 | echo "yes1:$INI__yes1" 34 | echo "true2:$INI__true2" 35 | echo "on3:$INI__on3" 36 | 37 | echo "no3:$INI__no3" 38 | echo "false2:$INI__false2" 39 | echo "off1:$INI__off1" 40 | 41 | 42 | # Third: with booleans processing explicity switched off via "--booleans 0" 43 | . ../read_ini.sh 44 | read_ini test6.ini --booleans 0 45 | 46 | echo "yes1:$INI__yes1" 47 | echo "true2:$INI__true2" 48 | echo "on3:$INI__on3" 49 | 50 | echo "no3:$INI__no3" 51 | echo "false2:$INI__false2" 52 | echo "off1:$INI__off1" 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /test/test7.ini: -------------------------------------------------------------------------------- 1 | 2 | ; Simple definitions for testing --prefix option 3 | 4 | var1=VAR 1 5 | 6 | [section1] 7 | var1 = "section 1 VAR 1" 8 | 9 | -------------------------------------------------------------------------------- /test/test7.out.correct: -------------------------------------------------------------------------------- 1 | # 1 2 | var1:VAR 1 3 | section1 var1:section 1 VAR 1 4 | # 2 5 | read_ini: invalid prefix 'PR:EFIX,' 6 | # 3 7 | read_ini: invalid prefix '1PREFIX' 8 | -------------------------------------------------------------------------------- /test/test7.sh: -------------------------------------------------------------------------------- 1 | 2 | # Test 7 3 | # 4 | # Option: [--prefix | -p] PREFIX 5 | 6 | # First: valid value for prefix 7 | echo "# 1" 8 | . ../read_ini.sh 9 | read_ini test7.ini --prefix PREFIX1 10 | 11 | echo "var1:$PREFIX1__var1" 12 | echo "section1 var1:$PREFIX1__section1__var1" 13 | 14 | # Second: invalid value for prefix (contains illegal chars) 15 | echo "# 2" 16 | . ../read_ini.sh 17 | read_ini -p PR:EFIX, test7.ini && 18 | { 19 | echo "var1:$PREFIX1__var1" 20 | echo "section1 var1:$PREFIX1__section1__var1" 21 | } 22 | 23 | # Third: invalid value for prefix (begins with a number) 24 | echo "# 3" 25 | . ../read_ini.sh 26 | read_ini --prefix 1PREFIX test7.ini && 27 | { 28 | echo "var1:$PREFIX1__var1" 29 | echo "section1 var1:$PREFIX1__section1__var1" 30 | } 31 | -------------------------------------------------------------------------------- /test/test8.ini: -------------------------------------------------------------------------------- 1 | 2 | ; this code injection via section worked on 0.3 3 | [x=blah; ls ;] 4 | var1 = blah 5 | -------------------------------------------------------------------------------- /test/test8.out.correct: -------------------------------------------------------------------------------- 1 | Error: Invalid line: 2 | 3: [x=blah; ls ;] 3 | -------------------------------------------------------------------------------- /test/test8.sh: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Test 8 4 | # 5 | # Prevention of in-value code execution via invalid section 6 | 7 | . ../read_ini.sh 8 | read_ini test8.ini 9 | 10 | -------------------------------------------------------------------------------- /test/test9.ini: -------------------------------------------------------------------------------- 1 | 2 | ; Testing quoted quotes (didnt't worked in 0.3) 3 | 4 | ; code injection 5 | var1="" ls "." 6 | var2='' ls '.' 7 | 8 | ; or just an error: unexpected EOF while looking for matching `"' 9 | var3=""" 10 | var4=''' 11 | var5=" 12 | var6=' 13 | -------------------------------------------------------------------------------- /test/test9.out.correct: -------------------------------------------------------------------------------- 1 | var1:" ls ". 2 | var2:' ls '. 3 | var3:" 4 | var4:' 5 | var5:" 6 | var6:' 7 | -------------------------------------------------------------------------------- /test/test9.sh: -------------------------------------------------------------------------------- 1 | 2 | # Test 9 3 | # 4 | # further stuff which didn't worked in 0.3 5 | 6 | . ../read_ini.sh 7 | if ! read_ini test9.ini ;then exit 1; fi 8 | 9 | 10 | echo "var1:$INI__var1" 11 | echo "var2:$INI__var2" 12 | echo "var3:$INI__var3" 13 | echo "var4:$INI__var4" 14 | echo "var5:$INI__var5" 15 | echo "var6:$INI__var6" 16 | 17 | --------------------------------------------------------------------------------