2

After I check if the ini file exists where it is expected to be, which I've learned how to do, I would like to check the file for proper formatting. I'd like to validate any number of "site" entries in the file and check that each entry adheres to the pattern below including the new line.

 1| [site1]
 2| shortcut=x1
 3| site=example1.com
 4| theme=alpha
 5|
 6| [site2]
 7| shortcut=x2
 8| site=example2.com
 9| theme=beta
10|  

I've got the individual values being validated as they are being read. I just want to make sure the pattern is correct before processing the file echoing the line number where the pattern breaks if it isn't.

  • so you want to check validity of .ini syntax right? – pqnet Aug 17 '14 at 19:41
  • You need a parser. – Braiam Aug 17 '14 at 20:04
  • Yes, that is my intent @pqnet . My apologies for the delay, I had to step out. – dimmech Aug 17 '14 at 21:22
  • write a parser in something other than bash. – Shadur Aug 18 '14 at 8:08
2

You could try crudini

crudini --get file.ini |
while read section; do
  test "$(crudini --get t.ini $section | paste -d, - - -)" = \
       'shortcut,site,theme' ||
    echo error in section $section
done
0

As a possible option:

BEGIN{
    FS="="
}
{
    if ($1~/\[.*\]/) {
        in_section=1;
        sec_name=$1
        next
    };
    if ($1~/^$/) {
        if ((c!=3) || (err)) {
            print "error in section "sec_name 
        };
        in_section=0; 
        sec_name="";
        c=0;
        err=0
    };
    if (in_section) {
        if ($1=="shortcut") {c++; next};
        if ($1=="site")     {c++; next};
        if ($1=="theme")    {c++; next};
        err=1
    };
}
END{
     if (in_section) {
        print "error in section "sec_name
    };
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.