Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-30-2018   -   Original Discussion by kchinnam
kchinnam's Unix or Linux Image
kchinnam kchinnam is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 18 June 2018, 1:26 PM EDT
Location: US
Posts: 204
Thanks: 38
Thanked 2 Times in 2 Posts
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+==

Sponsored Links
    #2  
Old Unix and Linux 04-30-2018   -   Original Discussion by kchinnam
rdrtx1's Unix or Linux Image
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
Join Date: Sep 2012
Last Activity: 31 May 2018, 9:41 AM EDT
Location: Houston, Texas, USA
Posts: 1,142
Thanks: 0
Thanked 421 Times in 398 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 04-30-2018   -   Original Discussion by kchinnam
kchinnam's Unix or Linux Image
kchinnam kchinnam is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 18 June 2018, 1:26 PM EDT
Location: US
Posts: 204
Thanks: 38
Thanked 2 Times in 2 Posts
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 Unix and Linux 04-30-2018   -   Original Discussion by kchinnam
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 13 June 2018, 6:37 PM EDT
Location: Saskatchewan
Posts: 22,696
Thanks: 1,183
Thanked 4,334 Times in 3,995 Posts
But you may be able to filter it into something you can use.
Sponsored Links
    #5  
Old Unix and Linux 04-30-2018   -   Original Discussion by kchinnam
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 21 June 2018, 11:38 PM EDT
Posts: 3,535
Thanks: 156
Thanked 1,261 Times in 1,155 Posts
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)
Sponsored Links
    #6  
Old Unix and Linux 04-30-2018   -   Original Discussion by kchinnam
kchinnam's Unix or Linux Image
kchinnam kchinnam is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 18 June 2018, 1:26 PM EDT
Location: US
Posts: 204
Thanks: 38
Thanked 2 Times in 2 Posts
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.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading .properties file on AIX and Windows shoefiend AIX 1 04-20-2012 10:38 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
Reading variable from file variable values sagii Shell Programming and Scripting 5 06-27-2009 01:06 PM



All times are GMT -4. The time now is 11:44 AM.