Bash: compress lines when reading from specific files

I have a web application that is deploying to a server. I am trying to create a script that other things read the current version of a web application from a properties file that is deployed with the application.

The file is as follows:

//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 try to concatenate strings (i.e. the last line above). In some cases, it works fine, but in other characters from one line, characters from another are placed instead of characters.

In these cases, it does not work :

  • When I read from the expanded file

:

  • .
  • .

. -, ?

/Ludwig

+3
2

, . , dos2unix .

, .

:

sed grep:

VERSION_LINE="$(sed -n "/version=/{s///;s/\r//g;p}" $VERSION_FILE)"

Bash, "version =".

grep , , , .

VERSION=${VERSION_LINE#$"version="}
VERSION=${VERSION//$'\r'}

, .

+1

foo.txt:

//other content
version=[version number]
build=[buildnumber]
//other content

awk:

awk -F'=' '$1 == "version" { version = $2}; $1 == "build" { build = $2}; END { print version"-"build}' foo.txt

, script . ?

:

, .

, ( , , , ).

Cheers,

+1

All Articles