I have a web application that is deployed to a server. I am trying to create a script that amoing other things reads the current version of the web application from a properties file that is deployed along with the application.
The file looks like this:
//other content
version=[version number]
build=[buildnumber]
//other content
I want to create a variable that looks like this: version-buildnumber
Here is my script for it:
VERSION_FILE=myfile
VERSION_LINE="$(grep "version=" $VERSION_FILE)"
VERSION=${VERSION_LINE#$"version="}
BUILDNUMBER_LINE=$(grep "build=" $VERSION_FILE)
BUILDNUMBER=${BUILDNUMBER_LINE#$"build="}
THEVERSION=${VERSION}-${BUILDNUMBER}
The strange thing is that this works in some cases but not in others.
The problem I get is when I am trying to concatenate the strings (i.e. the last line above). In some cases it works perfectly, but in others characters from one string replace the characters from the other instead of being placed afterwards.
It does not work in these cases:
- When I read from the deployed file
- If I copy the deployed file to another location and read from there
It does work in these cases:
- If I write a file from scratch and read from that one.
- If I create my own file and then copy the content from the deployed file into my created file.
I find this very strange. Is there someone out there recognizing this?
It is likely that your files have carriage returns in them. You can fix that by running
dos2unixon the file.You may also be able to do it on the fly on the strings you’re retrieving.
Here are a couple of ways:
Do it with
sedinstead ofgrep:and you won’t need the Bash parameter expansion to strip the “version=”.
OR
Do the
grepas you have it now and do a second parameter expansion to strip the carriage return.By the way, I recommend habitually using lowercase or mixed case variable names in order to reduce the chance of name collisions.