Here's a bash-with-embedded awk scrippet that parses Bacula .conf files. I don't know if it works with all possible permutations of .conf file layout (it's only tested on my own). It uses supporting functions ck_file, finalise and msg but they only do the obvious and are not significant in the parsing.
#-------------------------- # Name: parse_conf_file # Purpose: parses a conf file # * The conf file may be one of Bacula's or our own # Usage: # $1: name of file to parse # Global variables set: # keyword_orgs[] Keywords as they appear in the conf file # keywords[] Keywords after canonicalisation. "Keyword" incudes any { or }. # conf_value*[] Any values following a keyword (* is replaced by 0,1,2 ...) # conf_n_values[] Number of conf_value*[] arrays populated for each line # n_functional_lines Number of lines containing a keyword #-------------------------- function parse_conf_file { local conf_afn conf_afn=$1 # Does the file exist? # ~~~~~~~~~~~~~~~~~~~~ buf="$( ck_file $conf_afn:fr 2>&1 )" if [[ $buf != '' ]] then msg E "Terminating on configuration file problem:$lf$buf" finalise 1 fi eval "$( $awk ' BEGIN { squote = "\047" n_functional_lines=0 } { # Strip any comment and any spaces+tabs before it # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # This is not so easy because a # within a # quoted string does not introduce a comment and # an escaped " (that is \") does not terminate a # quoted string. in_string = 0 # False for ( i = 1; i <= length( $0 ); i++ ) { char = substr( $0, i, 1 ) if ( char == "#" && in_string == 0 ) { $0 = substr( $0, 1, i - 1 ) sub( /[ \t]*$/, "", $0 ) break } else if ( char == "\"" ) { if ( in_string == 0 ) in_string = 1 else if ( substr( $0, i - 1, 1 ) != "\\" ) in_string = 0 } } # Get keyword and value(s) string # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ split( $0, array, /[ \t]*=[ \t]*/ ) keyword_org = array[1] sub( /^[ \t]*/, "", keyword_org ) # Remove any leading spaces and tabs if ( keyword_org == "" ) next # Functionally empty line so discard print "keyword_orgs[" n_functional_lines "]=" squote keyword_org squote keyword = tolower( keyword_org ) gsub( /[ \t]*/, "", keyword ) # Remove any spaces and tabs from keyword print "keywords[" n_functional_lines "]=" squote keyword squote # Get individual values from value(s) string # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ n_values=0 values_string = array[2] while ( length( values_string ) > 0 ) { value = "" if ( substr( values_string, 1, 1 ) == "\"" ) { # Value is a quoted string buf = substr( values_string, 2, match( values_string, /[^\\]"/ ) - 1 ) # Strip quoted string just taken values_string = substr( values_string, length( buf ) + 3 ) # Copy to value, processing any escapes for ( i = 1; i <= length( buf ); i++ ) { char = substr( buf, i, 1 ) if ( char != "\\" ) value = value char else { # Backslash escape if ( substr( buf, i + 1, 1 ) == "\\" ) { # Escaped \ so keep one value = value "\\" i++ } } } } else { # Value is unquoted value = values_string sub( /[ \t].*$/, "", value ) # Strip anything after space or tab # Strip value string just taken values_string = substr( values_string, length( value ) + 1 ) } print "conf_value" n_values++ "[" n_functional_lines "]=" squote value squote # Clean up for the next loop pass # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ sub( /^[ \t]*/, "", values_string ) # Strip leading spaces and tabs } print "conf_n_values[" n_functional_lines "]=" squote n_values squote n_functional_lines++ } END { print "n_functional_lines=" squote n_functional_lines squote }' "$conf_afn" \ )" } # end of function parse_conf_file +---------------------------------------------------------------------- |This was sent by catki...@yahoo.co.uk via Backup Central. |Forward SPAM to ab...@backupcentral.com. +---------------------------------------------------------------------- ------------------------------------------------------------------------------ Nokia and AT&T present the 2010 Calling All Innovators-North America contest Create new apps & games for the Nokia N8 for consumers in U.S. and Canada $10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store http://p.sf.net/sfu/nokia-dev2dev _______________________________________________ Bacula-users mailing list Bacula-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bacula-users