0

Right now I am trying to right a script that will check the values from a .properties file against a group of set values in a script

But for some reason when I call the values they wont match up right, this is what I have so far, I believe maybe the .properties values are being stored right

#!/bin/bash 
SuccessfulDiffRun="true" 
timestamp() { date +"%a %d %b %Y"; }
TodaysDate=$(timestamp)
echo ""
echo $TodaysDate
echo ""
#Properties Call
file="savedState.properties" 
#Echo out the file
while read LINE; do echo "$LINE"; done < savedState.properties

#Check Values Hotpatch.
echo ""
echo "Running Checks HotPatch..."
if [ "$TodaysDate" = "$WD_MANAGEGOLD_DATETIMESTAMP" ]; then
    echo Dates Are A Match
    if [ "$SuccessfulDiffRun" = "$WD_MANAGEGOLD_SUCCESS" ]; then
        echo Diff Run Successful
        echo Hotpatch Run Was Successful
    else
        echo Diff Run Not Successful
        exit 0
    fi
else
    echo Dates Not A Match
    exit 0 
fi

#Check Values RC.
echo ""
echo "Running Checks RC..."
if [ "$TodaysDate" = "$WD_MANAGERC_DATETIMESTAMP" ]; then
    echo Dates Are A Match
    if [ "$SuccessfulDiffRun" = "$WD_MANAGERC_SUCCESS" ]; then
        echo Diff Run Successful
        echo RC Run Was Successful
    else
        echo Diff Run Not Successful
        exit 0
    fi
else
    echo Dates Not A Match
    exit 0 
fi

and the values in the .properties file

WD_MANAGEGOLD_DATETIMESTAMP=Wed 21 May 2015 
WD_MANAGEGOLD_SUCCESS=false
WD_MANAGERC_DATETIMESTAMP=Wed 21 May 2015 
WD_MANAGERC_SUCCESS=false

any help would be great

  • 1
    And when you do `source filename` or `. filename` in your present script to read the `WD_MANA...` files into your present script, I would recommend quoting the files in your .properties file (e.g. `WD_MANAGERC_DATETIMESTAMP="Wed 21 May 2015"` ) – David C. Rankin May 21 '15 at 09:03

4 Answers4

0

Make a function get_value that looks in the properties file.

function get_value {
   grep "^${1}=" savedState.properties | cut -d= -f2-
}
...
WD_MANAGERC_DATETIMESTAMP="$(get_value WD_MANAGERC_DATETIMESTAMP)"
Walter A
  • 19,067
  • 2
  • 23
  • 43
0

You have different options to make it working. First of all it's "Wed 21 May 2015" won't match with anything because it's "Thu 21 May 2015"... but that's a detail.

If you parse a file which contains bash variable definition you need to replace echo

...
while read LINE; do echo "$LINE"; done < savedState.properties
...

with eval (and eventually remove the ")

...
while read LINE; do eval $LINE; done < savedState.properties
...

You can laso replace the full line with a simple source savedState.properties

Finally protect the string definition in the properties file with "

WD_MANAGEGOLD_DATETIMESTAMP="Thu 21 May 2015"
WD_MANAGEGOLD_SUCCESS="false"
WD_MANAGERC_DATETIMESTAMP="Thu 21 May 2015"
WD_MANAGERC_SUCCESS="false"

Best, V.

Dr.Bokko
  • 116
  • 1
  • 5
0

Variables won't be created if yo just read the .properties file. Each line is stored in LINE, and if you want to execute the string as code, use eval:

while read LINE; do eval $LINE; done < savedState.properties

But make sure to quote the string which contains spaces inside, as

WD_MANAGEGOLD_DATETIMESTAMP="Wed 21 May 2015"
WD_MANAGEGOLD_SUCCESS=false
WD_MANAGERC_DATETIMESTAMP="Wed 21 May 2015"
WD_MANAGERC_SUCCESS=false

Another trick is just source the files as a script:

source savedState.properties

or

. savedState.properties

which are identical.

Besides, timestamp does not mean a string representing a date like 'Wed 21 May 2015'. Timestamp means the number of seconds(or milliseconds) from 01/01/1970. It's a number, not a string.

seven7e
  • 798
  • 1
  • 8
  • 19
  • When I try and use the source command, I get savedState.properties: line 1: 21: command not found, savedState.properties: line 3: 21: command not found any idea why ? –  May 21 '15 at 09:38
  • @ClarkPamler93 I tried on my machine and the `source` works, I guess you missed to quote `Wed 21 May 2015`. Because if you evaluate `xx=Wed 21`(instead of `xx="Wed 21"`, notice the quotes), `Wed` will be assigned to `xx` and `21` will be taken as a command. – seven7e May 21 '15 at 16:03
0

If you do source the .properties file (or whatever its final name) in your current script, the proper way to do it is to first check that it exists and is readable, then source:

#!/bin/bash
...
[ -r ".properties" ] && . ".properties" || {
    printf "error: source of file '.properties' failed, exiting"
    exit 1
}
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85