Skip to content

Instantly share code, notes, and snippets.

@marcelbirkner
Created March 7, 2016 13:19
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save marcelbirkner/9b133f800d7d3fc5d828 to your computer and use it in GitHub Desktop.
Save marcelbirkner/9b133f800d7d3fc5d828 to your computer and use it in GitHub Desktop.
Read property from properties file within Shell Script
#!/bin/sh
PROPERTY_FILE=apps.properties
function getProperty {
PROP_KEY=$1
PROP_VALUE=`cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2`
echo $PROP_VALUE
}
echo "# Reading property from $PROPERTY_FILE"
REPOSITORY_URL=$(getProperty "nexus.repository.url")
@trigolgy
Copy link

trigolgy commented Jun 5, 2018

good job! thanks..
and for me this line worked --> PROP_VALUE=$(cat $PROPERTY_FILE | grep $PROP_KEY | cut -d'=' -f2)

@ashutosh1211
Copy link

This worked for me on all *nix platform:
PROP_VALUE=cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2

@dhyaneshm
Copy link

you can just source the file
source apps.properties
echo $PROP_KEY should give you value

@adorogensky
Copy link

adorogensky commented Mar 6, 2019

you can just source the file
source apps.properties
echo $PROP_KEY should give you value

Will not work if any property key has dots, e.g. 'mail.alert.to=yourname@yourdomain.com'

Author's script won't work on property values that contain '=', case in point AES hash. Here is what I did,

get_property() {
        property=$(sed -n "/^[ tab]*$1[ tab]*/p" $2)
        if [[ $property =~ ^([ tab]*"$1"[ tab]*=)(.*) ]]; then
                echo ${BASH_REMATCH[2]}
        fi
}

@NielsHaar
Copy link

As @adorogensky mentioned this won't work if the string itself contains a '='. Simply change '-f2' to '-f2-'
echo "asd=asd=asd" | cut -d'=' -f2 -> "asd"
echo "asd=asd=asd" | cut -d'=' -f2- -> "asd=asd"

From the man page:

The list option argument is a comma or whitespace separated set of numbers and/or number ranges. Number ranges consist of a number, a dash (`-'), and a second number and select the fields or columns from the first number to the second, inclusive. Numbers or number ranges may be preceded by a dash, which selects all fields or columns from 1 to the last number. Numbers or number ranges may be followed by a dash, which selects all fields or columns from the last number to the end of the line. Numbers and number ranges may be repeated, overlapping, and in any order. If a field or column is specified multiple times, it will appear only once in the output. It is not an error to select fields or columns not present in the input line.

@dipendra-sharma
Copy link

`cat ${PROPERTY_FILE} | grep -w "$PROP_KEY" | cut -d'=' -f2``

All seems working for me. I did changes for Exact match of keys. grep with -w

@sshresthaEG
Copy link

It won't work when the property is commented out.

@hlulani-eoh
Copy link

hlulani-eoh commented Feb 1, 2021

The trouble with all these suggestions is that the resulting value could have a newline character.

cat ${PROPERTY_FILE} | grep -w version | cut -d '=' -f 2 | tr -d '\n'

@arvera
Copy link

arvera commented Apr 5, 2023

This script does not work when the variable and the value are the same.

prop1=prop1

@Mike4Online
Copy link

The trouble with all these suggestions is that the resulting value could have a newline character.

cat ${PROPERTY_FILE} | grep -w version | cut -d '=' -f 2 | tr -d '\n'

You can also strip out carriage returns as follows:

cat "${PROPERTY_FILE}" | grep -w version | cut -d '=' -f 2 | tr -d '\n\r'

tr will treat the '\n\r' specification as a list of characters to delete (in any order, whether alone or together). Handy if your property file was created on a Windows system.

This script does not work when the variable and the value are the same.

Try this:

cat "${PROPERTY_FILE}" | grep --color=never "prop1=" | cut -d '=' -f 2 | tr -d '\n\r'

Requires the = to immediately follow the property name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment