1

Right now I have a simple script I want to now call a .properties file and then print the results of that file, this is what I have so far

#Specific Set Variables 
SpecficStatus="Pass" #SetVariable 
timestamp() { date +"%a %d %b %Y %T %Z"; }
SpecficDate=$(timestamp)

echo $SpecficStatus
echo $SpecficDate

#Properties Call
file="savedState.properties"

#Echo out the file

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

Whats in my savedState.properties file

WD_MANAGEGOLD_DATETIMESTAMP=Wed 20 May 2015 13:50:51 UTC
WD_MANAGEGOLD_SUCCESS=false
WD_MANAGERC_DATETIMESTAMP=Wed 20 May 2015 13:50:51 UTC
WD_MANAGERC_SUCCESS=false

As you can see from the script I can call the file and print out the results but What I want is to be able to choose which values and store them as variables for later use in the script ??

Any help would be great

4
  • Could you paste the content of your savedState.properties file? It would help understand your ask better.
    – rahul
    May 20, 2015 at 15:19
  • @rahul I have updated the question with what you asked
    – Jamiex304
    May 20, 2015 at 15:21
  • Thanks. So you would like to choose only the MANAGEGOLD or MANAGERC related lines and save them as variables to be used later?
    – rahul
    May 20, 2015 at 15:29
  • well i was hoping to store all 4 as different variables so i would like to call each one on its own for example WD_MANAGERC_SUCCESS
    – Jamiex304
    May 20, 2015 at 15:34

2 Answers 2

2

You can export them like this.

#!/bin/bash
#Specific Set Variables 
SpecficStatus="Pass" #SetVariable 
timestamp() { date +"%a %d %b %Y %T %Z"; }
SpecficDate=$(timestamp)

echo $SpecficStatus
echo $SpecficDate

#Properties Call
file="savedState.properties"
#Echo out the file


while IFS== read -r VAR1 VAR2
do
    #echo "VAR1=$VAR1 : VAR2=$VAR2"
    export "$VAR1=$VAR2"
done < savedState.properties

echo "$WD_MANAGEGOLD_DATETIMESTAMP"
echo "$WD_MANAGEGOLD_SUCCESS"
echo "$WD_MANAGERC_DATETIMESTAMP"
echo "$WD_MANAGERC_SUCCESS"

This prints out :

Pass
Wed 20 May 2015 12:24:26 EDT
Wed 20 May 2015 13:50:51 UTC
false
Wed 20 May 2015 13:50:51 UTC
false
0
1

Using grep, to store e.g. WD_MANAGEGOLD_DATETIMESTAMP's value into a wd_managegold_datetimestamp variable and WD_MANAGEGOLD_SUCCESS's value into a wd_managegold_success variable:

#!/bin/bash

# ...
wd_managegold_datetimestamp=$(< savedState.properties grep -Po '^WD_MANAGEGOLD_DATETIMESTAMP=\K.*')
wd_managegold_success=$(< savedState.properties grep -Po '^WD_MANAGEGOLD_SUCCESS=\K.*')
# ...

But in this case it might be convenient to store related values into an array, so to store e.g. all WD_MANAGEGOLD and WD_MANAGERC values into two wd_managegold and wd_managerc arrays:

#!/bin/bash

# ...
IFS=$'\n\r' GLOBIGNORE='*' wd_managegold=($(< savedState.properties grep -Po '^WD_MANAGEGOLD_.*=\K.*'))
IFS=$'\n\r' GLOBIGNORE='*' wd_managerc=($(< savedState.properties grep -Po '^WD_MANAGERC_.*=\K.*'))
# ...
  • IFS=$'\n\r': Sets bash's internal field separator to both the newline character and the carriage return character, so that every newline / carriage return separated value gets stored into a single array's field;

  • GLOBIGNORE='*': Sets bash's builtin GLOBIGNORE variable to *, so that values matching special filenames won't be treated specially

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .