albfan / bash-ini-parser

GNU General Public License v3.0
141 stars 46 forks source link

getting unary operated expected #26

Closed siliconjesus closed 4 years ago

siliconjesus commented 6 years ago

Tried this on CentOS 6.9 (bash 4.1.2(2)) and MacOSX with bash installed (4.4.19(1)) via brew.

Sourced the script, then ran it against a config file I'm trying to parse with a format similar to the following:

[THING]

'# some commented out = things
something = somevalue
some other thing = some other value

[THING2]
something = not the same somevalue
some other thing = some other other value

I get the following error:

$ cfg_parser /etc/myfile/myfile.ini 
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: [: -v: unary operator expected
-bash: syntax error near unexpected token `('

I attempted to run the example in the script directory as well.

$ ./example.sh 
show example file

;seccion one: clean whitespace
[sec1]
       var1  =   foo
    var2       =hoge
    var3 = fuga
        var4=   pivo   
[sec2]
;this is the variable we want
var1=bar
var2=foo
var3=eco
;this is a multiword value
var4="piyo baz qux"
;this is an array
var5=foo bar baz hoge
var6=hoge

parsing file.ini
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected
../bash-ini-parser: line 8: [: -v: unary operator expected

show file parsed
[sec1]
var1="foo"
var2="hoge"
var3="fuga"
var4="pivo"
[sec2]
var1="bar"
var2="foo"
var3="eco"
var4="piyo baz qux"
;var5 is an array
var5="foo bar baz hoge"
var6="hoge"

show some results:

enable section 'sec2'
var2 value is "foo"
var5[1] value is "bar"
var5[*] value is "foo bar baz hoge"
var4 value is "piyo baz qux"

enable section 'sec1'
var2 value is "hoge"

update values:

update sec1.var1 to "foobar"
update sec2.var1 to "barfoo"
section cfg_section_sec1 not found
sec1.var1 value is "foobar"
section cfg_section_sec2 not found
sec2.var1 value is "barfoo"
albfan commented 6 years ago

bash 3.2 do not have the -v varname operator: to check if a variable is set. so I changed it to a bash [34].x compatible check.

That should fix the first part

-bash: [: -v: unary operator expected

Check it please

For the

-bash: syntax error near unexpected token `('

See #8. I recommend to close this and stick to that issue to fix that part

siliconjesus commented 6 years ago

For both instances, I'm using a 4.x version of bash.

GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html

This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.


$ bash --version GNU bash, version 4.4.19(1)-release (x86_64-apple-darwin17.3.0) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html

This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

albfan commented 6 years ago

-v conditional was included on bash 4.2 http://wiki.bash-hackers.org/scripting/bashchanges#conditional_expressions_and_test_command

As said that should be fixed on master

@siliconjesus for me is ok to open 2 issues for two different use cases, mix here CentOS and Mac only makes it hard to provide support.

I have brew too so I guess I can upgrade bash too. I suggest to keep this issue for CentOS and comment on #8 for MacOSX

albfan commented 4 years ago

As #8 is fixed and unary operator was already fixed on master I will close this issue.

NOTE: I don't have access to a bash 4.1, bash 4.2 so my suspiction that bash 3 was nongreedy on extglob and bash 4 was greedy, could be really bash >4.2 being nongreedy and <=4.2 being greedy.

script is now checking BASH_VERSINFO with has access to major and minor version.

If you are still interested in this issue and master still do not fix it, please try again and report the remaining fails, so we can tweak the inflexion point for this greedy behaviour.