Mismatched brackets

This is a simple bash script (suitable as a one-liner) for reading a fiel of source code and checking each line to ensure it has the same number of opening brackets as closing ones.

I found it useful for checking Asterisk dialplans, which can have horrible combinations of (), {} and [].

Note that in its current form, it simply adds up the total number of (, { or [ in a line and compares it to the total number of ), } or ], so a extra ( can be balanced by an extra } and won't be detected.

cat FileToBeChecked | while read line
do
  [ "`echo \"$line\" | tr -cd '[{(' | wc -c`" != "`echo \"$line\" | tr -cd ')}]' | wc -c`" ] && echo "$line"
done

Go up
Return to main index.