Keyur
Keyur

Reputation: 399

Evaluating String from configuration file in unix

I have a string in my configuration file say something like

 FirstParam|MyFile`date +"%Y%m%d"`.xml

My Script reads this line and splits values by pipe (e.g. "|") I want to convert second part i.e. MyFile`date +"%Y%m%d"`.xml to MyFile20141215.xml and pass it to another method. What is the best possible way for achieving this?

Thanks in advance.

Upvotes: 0

Views: 44

Answers (2)

Walter A
Walter A

Reputation: 20022

When you are concerned with security, you want to avoid evaluating 'external' commands. When the config file is just as secure as your script, eval is fine. When the config file can be changed by people with less rights, consider parsing the string with special fields:

$-> cat myfile
FirstParam|MyFileSED_YYYYMMDD.xml

$-> cat myParser
cat myfile | while IFS=\|; read firstfield secondfield; do
        echo Split into ${firstfield} en ${secondfield}
        echo "callingOtherMethodWith $(echo ${secondfield} |
           sed 's/SED_YYYYMMDD/'$(date +"%Y%m%d")'/')"
done

Upvotes: 0

Davide
Davide

Reputation: 508

The one quick and dirty I would use is (supposing a.txt is your file)

TodayFileName=`cut -d\| -f2 a.txt`
eval TodayFileName=$TodayFileName

Upvotes: 1

Related Questions