Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

linux - read key-value from file using shell script

I have one of configuration file e.g. called (abc.conf)

abc.conf
------- 
location1=/opt/test1/locationx
location2=/opt/test1/locationy
location3=/opt/test1/locationz

I have sample code which works fine if I am not using such path and slashes

getProperty()
{
PROPERTY_FILE=$1
PROP_KEY=$2
PROP_VALUE=`cat $PROPERTY_FILE | grep -w "$PROP_KEY" | cut -d'=' -f2`
echo $PROP_VALUE
}

Using like

VALUE=$(getProperty $CONF_FILE $LOCATION1)

But this function have limitation using it, when I use it like to fetch location1 it also fetches location2/location3 like similar location together instead specific key value only. it should work with unique value only.

question from:https://stackoverflow.com/questions/65844954/read-key-value-from-file-using-shell-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This code is working.

Calling function,

$ getProperty abc.conf location1(2 or 3)
/opt/test1/locationx

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...