-1

I am sourcing the property files like . PATH_TO_PROPERTYFILE. If, after reading the values I change one, how do I update the the original file to reflect that modified value?

I tried $SOURCED_ARRY_NAME = ("${TEMP_ARRAY[@]}") but this did not help. Also i want the changes to updated back in the properties file. This is basically accessing the array from property file and modifying it.

file name format : abc.properties

services_deployed=()

xyzapps_deployed=()
vthallam
  • 551
  • 2
  • 8
  • 20
  • 1
    BTW, if `TEMP_ARRAY` is one you're setting yourself, note that all-caps variable names are reserved for system use, so it's bad practice to reuse them for your own variables; see fourth paragraph of spec @ http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html, noting that shell variables and environment variables share a namespace. ("The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities.") – Charles Duffy Jan 25 '16 at 22:43
  • Do you have a complete list of the variables which should be set in that file? Alternately, do they all have a common prefix? – Charles Duffy Jan 25 '16 at 23:22
  • They don't have a common prefix, but i do have the list of variables to be updated! – vthallam Jan 25 '16 at 23:24
  • Show code. And it depends on the format of the "property file". Show content. – Karoly Horvath Jan 25 '16 at 23:26
  • ...since your "property file" is written in shell syntax, it's dangerous to assume that every line has a single `key=value` pair, or even that any line that *looks like* `key=value` is in fact an assignment (as opposed to part of a heredoc, a multi-line string, or anything else)... hence my preference to rewrite from scratch every time, which is less likely to generate invalid syntax and, if it throws away conditional logic someone adds to the configuration, will do so in a way that makes it obvious what's happening. – Charles Duffy Jan 25 '16 at 23:29
  • Updated the question with the sample file name and structure! – vthallam Jan 25 '16 at 23:29
  • ...btw, if you know that you want to write out any variable whose name ends in `_deployed`, that's possible too. (A suffix search isn't quite as easy as a prefix search, but it's very much doable). – Charles Duffy Jan 25 '16 at 23:31

1 Answers1

1

You can generate code which sets a variable to its current value with declare -p.

Thus:

declare -p SOURCED_ARRAY_NAME >>"PROPERTYFILE"

...will append code which sets SOURCED_ARRAY_NAME to its new value to the end of the file named PROPERTYFILE.


Thus, if you know the full list of variable names which can be set in your configuration file, you can do this for all of them:

vars=( SOURCED_ARRAY_NAME othervar1 othervar2 othervar3 )
write_config() {
  local varname
  for varname in "${vars[@]}"; do
    [[ ${!varname+set} ]] && declare -p "$varname"
  done 
}

write_config >"propertyfile"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • I guess this does the job for me! If i understand it correctly, i form the arrays and append these arrays to the property file instead of reading it and updating it. Am i correct in understanding? – vthallam Jan 25 '16 at 23:32
  • I would actually tend to suggest fully rewriting the file -- from the beginning -- if you can get away with it. Appending leaves you with the problem of having a value set multiple times (with only the last one counting), and the file's length continually growing. – Charles Duffy Jan 25 '16 at 23:34