If the file only contains those five lines, then another option is to read it into your script, alter the contents directly, then re-export it.
One other question I have though is how you wish to decide which script parameters change which variable setting. One very simple option could be to just insert blank entries on the command line for the ones you don't want to change.
Code:
#!/bin/bash
file=inject.dat
# confirm that the file is readable first.
if [[ ! -r $file ]]; then
echo "$file is not readable. Exiting."
exit 1
fi
# import each line and split it at the "=" sign into two temporary variables.
# then test the first part and save the second part into the appropriate variable.
while IFS='=' read -r name value ; do
case $name in
PKGNAME) pkgname=$value ;;
OLD_VERSION) oldvers=$value ;;
NEW_VERSION) newvers=$value ;;
"PRODUCT NUMBER") prodnum=$value ;;
"RELEASE DATE") reldate=$value ;;
esac
# export the values back into the file. If a new entry parameter exists,
# for that value then use it, otherwise default to the original.
{
echo "PKGNAME=${1:-$pkgname}"
echo "OLD_VERSION=${2:-$oldvers}"
echo "NEW_VERSION=${3:-$newvers}"
echo "PRODUCT NUMBER=${4:-$prodnum}"
echo "RELEASE DATE=${5:-$reldate}"
} >"$file"
exit 0
Then, to use the script, do something like this:
Code:
./updatescript '' '' 1.0.2 '' 05/10/2012
This will update the new version number and release date, an keep the package name, old version number, and product number the same.
Of course this means you have to remember the exact order to put them in. You may want to create a more complex and robust system. Perhaps something with getopts, or even an interactive version using read.
BTW, if you can ensure that the lines always match proper variable form (var=value, with no spaces), then the whole thing can be made even easier. Just
source the file and use the entries themselves as the variables.
Code:
#!/bin/bash
file=inject.dat
# if the file is readable, source it into the script.
if [[ ! -r $file ]]; then
. "$file
else
echo "$file is not readable. Exiting."
exit 1
fi
# export the values back into the file. If a new entry exists as a parameter,
# use it, otherwise default to the original.
{
echo "PKGNAME=${1:-$PKGNAME}"
echo "OLD_VERSION=${2:-$OLD_VERSION}"
echo "NEW_VERSION=${3:-$NEW_VERSION}"
echo "PRODUCT_NUMBER=${4:-$PRODUCT_NUMBER}"
echo "RELEASE_DATE=${5:-$RELEASE_DATE}"
} >"$file"
exit 0