I would like to know how to bump the last digit in a version number using bash.
e.g.
VERSION=1.9.0.9
NEXT_VERSION=1.9.0.10
EDIT: The version number will only contain natural numbers.
Can the solution be generic to handle any number of parts in a version number.
e.g.
1.2
1.2.3
1.2.3.4
1.2.3.4.5
TL;DR:
For a detailed explanation, read on.
Let’s start with the basic answer by froogz3301:
How can we improve on this? Here are a bunch of ideas extracted from the copious set of comments.
The trailing ‘1’ in the program is crucial to its operation, but it is not the most explicit way of doing things. The odd ‘1’ at the end is a boolean value that is true, and therefore matches every line and triggers the default action (since there is no action inside braces after it) which is to print $0, the line read, as amended by the previous command.
Hence, why not this
awkcommand, which obviates thesedcommand?Of course, we could refine things further — in several stages. You could use the bash ‘<<<‘ string redirection operator to avoid the pipe:
The next observation would be that given a series of lines, a single execution of awk could handle them all:
without the for loop. The double quotes around "$VERSION" preserve the newlines in the string. The pipe is still unnecessary, leading to:
The regex ignores the blank lines in
$VERSIONby only processing lines that contain a digit followed by a dot. Of course, setting OFS in each line is a tad clumsy, and ‘+=1‘ can be abbreviated ‘++‘, so you could use:(or you could include ‘
BEGIN{OFS="."}‘ in the program, but that is rather verbose.The ‘
<<<‘ notation is only supported by Bash and not by Korn, Bourne or other POSIX shells (except as a non-standard extension parallelling the Bash notation). The AWK program is going to be supported by any version ofawkyou are likely to be able to lay hands on (but the variable assignment on the command line was not supported by old UNIX 7th Edition AWK).