Home > Software design >  read properties with '=' and white spaces from app.properties in unix shell script
read properties with '=' and white spaces from app.properties in unix shell script

Time:01-21

I'm reading the properties as shown below, but because I'm using the = sign, it's breaking. How can I read the full property? In property file dbName=jdbc://sample:8080;nameSpace=name1; --config host=sample

# Script used to read Property File
FILE_NAME=Test.prop
# Key in Property File
key="dbName"

# Variable to hold the Property Value
prop_value=""

getProperty()
{
        prop_key=$1
        prop_value=`cat ${FILE_NAME} | grep ${prop_key} | cut -d'=' -f2`
}

getProperty ${key}
echo "Key = ${key} ; Value = " ${prop_value}

CodePudding user response:

Simply remove all text up to the first =.

prop_value=$(grep "^${prop_key}=" ${FILE_NAME} | sed 's/[^=]*=//')

Notes:
You rarely need cat for the input files. I modified the grep pattern to match the complete key at the beginning of the line only. This avoids matches for lines that may contain the specified key as a substring of the key or value. I replaced the backticks with $(...).

  •  Tags:  
  • Related