fzkl
fzkl

Reputation: 1007

How do I improve my logic to read and write variable values to a file?

I wrote to functions in shell script to read and write variable settings from/to a file. I am wondering if there is a cleaner way to do this. I especially don't like the bit where I delete the existing match for a write and then rewrite at the end of the file. Is there a better way to do this? Thanks.

fnReadTestSetting()
{
#$1 is the variable to be read from the testsettings.ini file
grep $1 $MAIN_FOLDER/testsettings.ini >$MAIN_FOLDER/temp.ini
if [ $? -eq 0 ]; then
    sed -i "s|$1=||" $MAIN_FOLDER/temp.ini
    TEST_VALUE=`cat $MAIN_FOLDER/temp.ini`
    export $1=$TEST_VALUE
fi
rm $MAIN_FOLDER/temp.ini
return
}


fnWriteTestSetting()
{
#$1 is the variable and value to be written to the testsettings.ini file in the format VARIABLE_NAME=VALUE
if [ ! -e $MAIN_FOLDER/testsettings.ini ]; then
    touch $MAIN_FOLDER/testsettings.ini
fi
SETTING_NAME=`echo $1 | awk -F = '{print $1}'`
grep $SETTING_NAME $MAIN_FOLDER/testsettings.ini &>/dev/null
if [ $? -eq 0 ]; then
    sed -i "/^$SETTING_NAME/d" $MAIN_FOLDER/testsettings.ini
    echo "$1" >> $MAIN_FOLDER/testsettings.ini

else
    echo "$1" >> $MAIN_FOLDER/testsettings.ini
fi
return
}

Upvotes: 2

Views: 866

Answers (2)

dogbane
dogbane

Reputation: 274612

I have made some improvements:

You can use a single sed for reading a setting. This saves writing to a tmp file. Not sure why you were trying to export $1.

fnReadTestSetting()
{
  #$1 is the variable to be read from the testsettings.ini file
  sed -n "s/^$1=\(.*$\)/\1/p" $MAIN_FOLDER/testsettings.ini 
}

For writing, you don't need to touch the file if it doesn't exist, because it will be created anyway. You don't need awk for finding the setting name. You can also place grep as the condition in your if-statement instead of explicitly checking its exit code. Use a single sed for the replacement (instead of deleting and echoing).

fnWriteTestSetting()
{
  #$1 is the variable and value to be written to the testsettings.ini file in the format VARIABLE_NAME=VALUE
  SETTING_NAME=${1%%=*}
  if grep -sq "^${SETTING_NAME}=" $MAIN_FOLDER/testsettings.ini
  then
    sed -i "s/^${SETTING_NAME}=.*$/$1/" $MAIN_FOLDER/testsettings.ini
  else
    echo "$1" >> $MAIN_FOLDER/testsettings.ini
  fi
  return
}

Update:

%%=* deletes everything after the =. For more info on the %% operator take a look at the string manipulation guide:

${string%%substring} Deletes longest match of $substring from back of $string.

Upvotes: 3

Ben Taitelbaum
Ben Taitelbaum

Reputation: 7403

There's really no need to use sed -i on a temp file, since you can just use sed to return the string that you're setting to a variable anyway. Also, I don't believe your regex accounts for space on either side of the equals sign, which is often allowed in .ini files ( http://en.wikipedia.org/wiki/INI_file ). The regex is still pretty clean, and not writing to temp files is much cleaner, as it makes the read function non-side-effecting.

Upvotes: 0

Related Questions