Created
March 7, 2016 13:19
-
-
Save marcelbirkner/9b133f800d7d3fc5d828 to your computer and use it in GitHub Desktop.
Read property from properties file within Shell Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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") |
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
This script does not work when the variable and the value are the same.
prop1=prop1