How to walk through a config file with /bin/sh

Hi there,

i tried a lot of things actually to get my script running as expected.

Situation:
I have a config file with several parameters in the form

     FFF.thisIsaParam=Value
     FFF.included.AnotherPortofParam=Value

Additionally there are some comments in it.

Script should be in traditional /bin/sh

running a for-loop works as long as I filter out the commented lines, which i not what I need.
Going for a "while read line" statement does print the comments and writes them to the tempfile but I'm not able to enter/modify the values param by param, as the additional read statement is beeing "ignored".

The easiest way would be some kind of array, but I didn't got it working so far. Is there another way to get this working?

Thanks and regards,
Stefan

Please post an example of your input and the desired output. When doing so use

```text
 and 
```

tags to enhance readability and to preserve formatting like indention etc., ty.

Hi again,

no problem. Here are the first lines of the properties file which is the input:

#--------------------Common WebMAX Service Settings--------------------
Max.defBpId=CH999148
Max.env=T
Max.exceptionFile=exception.txt
Max.fileRoot=/opt/SISwebMAX/webmaxTest/Service
Max.logRoot=/opt/SISwebMAX/webmaxTest/Service/logs
Max.reportRoot=/opt/SISwebMAX/webmaxTest/Service/reports
Max.loginBased=Y

#--------------------Trace Logging settings--------------------
Max.traceFile=trace.txt
Max.traceLevel=ALL
Max.tracing=ON

#--------------------TCP Port Settings--------------------
Max.useCc=N
Max.ccPort=2301

Here is what the interactive shell should show:

bash-3.00$ ./prop_test.sh
Do you install in test or prod? prod

Set Max.defBpId - Actual (CH999148):
Set Max.env - Actual (T):
Set Max.exceptionFile - Actual (exception.txt):
Set Max.fileRoot - Actual (/opt/SISwebMAX/webmaxTest/Service):
....

The properties file is used by a java based application.

Regards,
Stefan

So if I understood it correct, you have problems with the substitution in the file.
If you have no problem to set up the while/read loop to get the wanted input, you can use the values stored in variables with for example sed to exchange the current values in the config file with the new ones.
Here a general example how substitute patterns:

# you got VAR filled with a value by your former read/while loop of course
sed "s/oldvalue/${VAR}/g"

Do this for several of your patterns and write it to a tmp file and maybe mv it over the original. Could take a backup before doing it.

Hi zaxxon,

thanks for your answer and your help trying to get the goal.

My problem is not, to exchange value "a" with value "b", but the interactive part above. The example there is how the prompts should look like. but doing this with a "while read" loop fails.

This is what I've created:

while read line
do
  comment=`echo $line | sed -e 's/#.*/#/'`
  if [ $comment = "#" ]; then
      echo "$line" | sed -n 's/#.*/&/p' >>$tmpfile
  else
     cf_key=`echo $line | awk 'BEGIN {FS = "="} {print $1}'`
     cf_val=`echo $line | awk 'BEGIN {FS = "="} {print $2}'`
     printf "Set $cf_key - Actual ($cf_val): \n"
     read value
     if [ $value = "" ]; then
        echo "$cf_key=$cf_val" >>$tmpfile
     else
        echo "$cf_key=$value" >>$tmpfile
     fi
  fi 
done < $config

Running this will result in several lines of output on screen, but no input reading prompt (read value, get ignored).
My question is: Is there another way to get this working?

The for-loop is not an option as I've written at the begin, because it does'nt handle the file line by line (whitespace problem). Except if ther's a way to advise the for-loop to ignore the whitespace. I've alread played around with setting of IFS, but I didn't come any further.

Best regards,
Stefan

there was already an input stream being put to by "< $config"
is it possible for you to not loop in the config file but add the parameters in the script itself?

http://www.unix.com/shell-programming-scripting/20828-input-inside-while-read-loop.html

see the above thread.

#!/bin/ksh

echo "Enter config file path:"
read config

echo "Enter tmp file path:"
read tmpfile

rm $tmpfile

while read line
do
  comment=`echo $line | sed -e 's/#.*/#/'`
  if [ $comment = "#" ]; then
      echo "$line" | sed -n 's/#.*/&/p' >>$tmpfile
  else
     cf_key=`echo $line | awk 'BEGIN {FS = "="} {print $1}'`
     cf_val=`echo $line | awk 'BEGIN {FS = "="} {print $2}'`
     printf "Set $cf_key - Actual ($cf_val): \n"
     read value </dev/tty 2>/dev/tty
     echo "Value read is $value"
     if [ $value = "" ]; then
        echo "$cf_key=$cf_val" >>$tmpfile
     else
        echo "$cf_key=$value" >>$tmpfile
     fi
  fi
done < $config

Hi friends,

yes that's the trick and does work well. Very cool! Many thanks for your help.

Regards,
Stefan

This shorter nawk trick should also work:

BEGIN{ FS=OFS="="}
/^#|^$/ {print; print > "/tmp/new_config"}
!/^#|^$/ {
	print "Set "$1 " - actual  (" $2 "):"
	"read value; echo $value" | getline myvalue
	print  $1,myvalue > "/tmp/new_config"
	close("read value; echo $value")
}