how to read a variable from a file to a Unix scrip

2019-09-20 14:39发布

问题:

I have a text file as below:

[Global]

$DBConnection_prd_load=GETDB

$$SURVEY_TYPE=ALL

$$VENDOR_SURVEY_ID=5

$$PROD_EMAIL=users@tnnnet.com

$$LASTRUNDATE=10/24/2013

I want to pass above file $$LASTRUNDATE value to below script. Pl help. And also replace / with _.

#! /bin/ksh

export SRCDIR=/informat/PowerCenter/9.1.0/server

export TGTDIR=/informat/PowerCenter/9.1.0/server/infa

export FILEDT=**$$LASTRUNDATE**

cd ${SRCDIR}

export GET_FNAME=AllResponses_${FILE_NUM}_${FILEDT}*.txt

        if [ -f ${GET_FNAME} ]; then

                cp ${SRCDIR}/${GET_FNAME} ${TGTDIR}

Can you pl also let me know how to replace '\' with '_' ?

Friend..one more small favour..is it possible to add 1 day to date that we get in the above variable..?

回答1:

You could just use backticks.

This would produce the output:

cat mytextfile | grep '\$\$LASTRUNDATE=' | sed 's/\$\$LASTRUNDATE=//g'| sed 's/\//_/g'

And this would allow me to store it:

export FILEDT=`cat mytextfile | grep '\$\$LASTRUNDATE=' | sed 's/\$\$LASTRUNDATE=//g' | sed 's/\//_/g'`


回答2:

This will give you value in required format :

grep '$$LASTRUNDATE' <file-name> | cut -d= -f2 | sed -e 's/\//\-/g'


标签: shell unix ksh