Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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 09: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 10:58 PM.. Reason: Fix ICODE tags.
The Following 2 Users Say Thank You to Chubler_XL For This Useful Post:
kchinnam (04-30-2018), rbatte1 (05-01-2018)
# 6  
Old 04-30-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

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading multiple values from multiple lines and columns and setting them to unique variables. FMMOLA Shell Programming and Scripting 7 05-23-2014 02:42 PM
Reading .properties file on AIX and Windows shoefiend AIX 1 04-20-2012 10:38 AM
Reading command line arguments and setting up values if option not provided kristinu Programming 2 03-22-2012 09:16 PM
Reading values from a file lost.identity Shell Programming and Scripting 1 11-24-2011 06:57 AM
reading in properties file vsekvsek Shell Programming and Scripting 2 11-12-2010 05:54 PM
Reading the Properties File From Shell script venukjs Shell Programming and Scripting 1 08-13-2010 08:16 AM
Problem with reading from a properties file sailaja_80 Shell Programming and Scripting 6 08-21-2009 04:58 PM
Problem with retrieving values from properties file sailaja_80 Shell Programming and Scripting 2 08-21-2009 03:06 PM
Reading variable from file variable values sagii Shell Programming and Scripting 5 06-27-2009 01:06 PM
Setting properties for vi during run time. ashish_uiit Shell Programming and Scripting 4 08-14-2008 11:29 AM


All times are GMT -4. The time now is 12:56 PM.

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