Saturday, June 23, 2012

HowTo - read a value from a Java Properties file in Bash

    # Reads property $2 from properties file $1 and echos the value. To call this method do:
    #
    #     V=$(getProp filename property)
    #
    function getProp () {
        # ignore lines with '#' as the first non-space character (comments)
        # grep for the property we want
        # get the last match just in case
        # strip the "property=" part but leave any '=' characters in the value

        echo `sed '/^[[:space:]]*\#/d' $1 | grep $2  | tail -n 1 | cut -d "=" -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'`
    }

No comments: