Reading properties from file and setting variable values

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 04-30-2018
Reading properties from file and setting variable values

I want to read properties from a file and print evaluated values of each key.
I am using AIX6.1.


Code:
 
 myfile.props
protocol=http
siteA.host=siteAhostname
pageA=pageNameA
siteAURL1=${protocol}:/${siteA.host}/pagea/blabla?v1=32
siteAURL2=${protocol}:/${siteA.host}/${pageA}/blabla?v1=32
test.enc=AFSAF!$fsafasf+==

Code:
 
 evalnprint.sh
#!/bin/bash
. ./myfile.props

Code:
./evalnprint.sh
./abc.props: line 2: siteA.host=siteAhostname: command not found
./abc.props: line 4: ${protocol}:/${siteA.host}/pagea/blabla?v1=32: bad substitution
./abc.props: line 5: ${protocol}:/${siteA.host}/${pageA}/blabla?v1=32: bad substitution
./abc.props: line 6: test.enc=AFSAF!+==: command not found

Expected output
Code:
http
siteAhostname
pageNameA
http://siteAhostname/pagea/blabla?v1=32
http://siteAhostname/pageNameA/blabla?v1=32
AFSAF!$fsafasf+==

# 2  
Old 04-30-2018
change all occurrences of siteA.host to siteA_host
change all occurrences of test.enc to test_enc

In other words, do not use "." as part of a variable name.
# 3  
Old 04-30-2018
props are defined for huge real world app, I can't change existing format.
If its just keyName I would have replaced '.' with '_' and back. values have all kinds of special characters like '='

Last edited by kchinnam; 04-30-2018 at 10:56 PM.. Reason: better reason
# 4  
Old 04-30-2018
But you may be able to filter it into something you can use.
# 5  
Old 04-30-2018
You could write a load-prop function that replaces all non-alphanumeric characters in the variable names with underscore like this:

Code:
#!/bin/bash
function load-prop {
 . <(
  awk '
    FNR==NR {
       p=index($0, "=")
       if (!p) next
       VAR=substr($0,1,p-1)
       NEW=VAR
       while (match(NEW, "[^_A-Za-z0-9]"))
          NEW=substr(NEW, 0, RSTART - 1) "_" substr(NEW, RSTART + 1)
       if(NEW != VAR) repl[VAR]=NEW
       next
    }
    {
       for(rep in repl)
          gsub(rep, repl[rep], $0)
    }
    1
' $1 $1)
}

load-prop ./myfile.props

echo "SITEAURL2 is $siteAURL2"
echo "test.enc is $test_enc"

Output:
Code:
SITEAURL2 is http:/siteAhostname/pageNameA/blabla?v1=32
test.enc is AFSAF!+==


Edit:

Just noticed that test.enc has had the string $fsafasf incorrectly expanded by the shell.
Single quotes could avoid this issue, easy if you can update your prop file(s).

The awk script could adjust for this automatically, but it needs to identify when this is needed

If all variables use the ${} format then $ characters without a following { could be used to trigger the quoting eg:
Code:
test_enc='AFSAF!$fsafasf+=='

You may also have issues with spaces within the variable values.

Last edited by Don Cragun; 04-30-2018 at 11:58 PM.. Reason: Fix ICODE tags.
These 2 Users Gave Thanks to Chubler_XL For This Post:
kchinnam (05-01-2018), rbatte1 (05-01-2018)
# 6  
Old 05-01-2018
Thanks XL. I do not see single quotes in properties, I will try to wrap values in single quotes first, then use your script to expand variable substitution. Thanks for taking time to look into it.
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading Xml file and print the values into the text file in columnwise? sravanreddy UNIX for Dummies Questions & Answers 4 07-23-2015 04:13 AM
Reading multiple values from multiple lines and columns and setting them to unique variables. FMMOLA Shell Programming and Scripting 7 05-23-2014 03:42 PM
Reading and Comparing values of file azherkn3 Shell Programming and Scripting 4 02-07-2014 03:00 AM
Reading off values from a large file lost.identity Shell Programming and Scripting 6 10-14-2013 10:09 AM
Reading .properties file on AIX and Windows shoefiend AIX 1 04-20-2012 11:38 AM
Reading command line arguments and setting up values if option not provided kristinu Programming 2 03-22-2012 10:16 PM
Reading values from a file lost.identity Shell Programming and Scripting 1 11-24-2011 07:57 AM
reading in properties file vsekvsek Shell Programming and Scripting 2 11-12-2010 06:54 PM
Reading the Properties File From Shell script venukjs Shell Programming and Scripting 1 08-13-2010 09:16 AM
Problem with reading from a properties file sailaja_80 Shell Programming and Scripting 6 08-21-2009 05:58 PM
Problem with retrieving values from properties file sailaja_80 Shell Programming and Scripting 2 08-21-2009 04:06 PM
Reading variable from file variable values sagii Shell Programming and Scripting 5 06-27-2009 02:06 PM
Reading from a file and assigning values ssuresh1999 UNIX for Dummies Questions & Answers 6 08-17-2008 11:25 PM
Setting properties for vi during run time. ashish_uiit Shell Programming and Scripting 4 08-14-2008 12:29 PM
All times are GMT -4. The time now is 03:03 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?