7

I have a bash file and I want to store a version text in it (preferably "echo version 1.0.1" so that it automatically prints out its own version when executed)

I am trying to make a command group that will update the version.

So far I've got

grep "echo version" ~/.bashrc | cut -c 14- | sed -e 's/\./\n/g'

which extracts "echo version 1.0.1", cuts out the "echo version " and splits the version between the dots

1
0
1

and I'm hitting my limit of regex, bash, and google-foo to extract the last number (patch version) which I could increment and write back to file.

Mainly, how do I get that last number?

Also if there's anything much better I should be doing, please do suggest.

3 Answers 3

6
awk -F'[ .]' '/^echo version/ {print $1,$2,$3"."$4"."$5+1}' ~/.bashrc

Output:

echo version 1.0.2

-F'[ .]': field separator(s) blank and dot

/^echo version/: search for line starting (^) with echo version

print $1,$2: print strings separated by a blank

$5+1: increment string/value by 1

4

perl to the rescue

$ cat file
echo version 1.0.1
$ perl -i -pe 's/echo version \d+\.\d+\.\K(\d+)/ $1+1 /e' file
$ cat file
echo version 1.0.2
1

Use bash-semver:

https://github.com/fmahnke/shell-semver

## Use bash-semver
## https://github.com/fmahnke/shell-semver
#===========

#Increment semantic versioning strings in shell scripts.
./increment_version.sh
usage: increment_version.sh [-Mmp] major.minor.patch

./increment_version.sh -p 0.0.0
#0.0.1

./increment_version.sh -m 0.0.3
#0.1.0

./increment_version.sh -M 1.1.15
#2.0.0

./increment_version.sh -Mmp 2.3.4
#3.1.1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .