Incremental logic

Hi Guys,
am trying to write logic to do incremental value using linux
Example:

a=00.00.00.01

My b should be like this

b=00.00.00.02

and when it reaches 99 my b should look like this

b=00.00.01.99

Appreciate your help guys

Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework Questions forum under special homework rules.

If this is not a homework assignment, please explain the company you work for and the nature of the problem you are working on. And, please show us what you have tried to solve this problem on your own (in CODE tags).

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Hi , Am trying bump the SNAPSHOT version in pom.xml , My company is APERVI
This is what i tried

 a=00.00.00.01
 b=`echo $a | cut -d"." -f1,2,3`"."`expr \`echo $a | cut -d"." -f4\` + 1`
echo $b
00.00.00.2

Here is a tutorial showing how to use CODE and ICODE tags:

What operating system and shell are you using?

Have you considered using awk ?

Do you really want to go from 00.00.00.99 to 00.00.01.99 or do you really want to go to 00.00.01.00 ? That is, is the intent to always have each element of the version number be a two-digit number where low order elements count from 00 through 99 and then resets to 00 when incrementing the next higher element? Or, does an element of the version number lock at 99 when the next higher order element increments from 00 through 99 as shown in post #1?

No I haven't tried awk , I think I would like to go 00.00.01.00 once it reached 00.00.00.99

I repeat: "What operating system and shell are you using?"

And, where is this version number stored? Are you reading it from a file, or is it stored in a variable in your shell script?

am reading it from pom.xml, lets say original one is [project-SNAPSHOT-00.00.00.01]
And i should change that to [project-SNAPSHOT-00.00.00.02] push back git

Assuming bash being your shell:

b=$(printf "%08d" $(( 10#${b//./} + 1 )) | fold -w2 | paste -sd.)

used in a loop:

00.00.00.98
00.00.00.99
00.00.01.00
00.00.01.01

Supplying ALL relevant info (e.g. as requested by Don Cragun) REALLY helps people help you!

[a=00.00.00.01]

am getting error in this

b=$(printf "%08d" $(( 10#${b//./} + 1 )) | fold -w2 | paste -sd.)
usage: paste [-s] [-d delimiters] file ...

---------- Post updated at 10:19 AM ---------- Previous update was at 10:18 AM ----------

yes am sorry am using bash

Try:

paste -sd. -

But I want to read it from [a=00.00.00.12] and want to make that in next iteration

Adapting RudiC's suggestion

b=$(printf "%08d" $(( 10#${a//.} + 1 )) | fold -w2 | paste -sd. -)

Got it worked thanks guys .. Next time if i want to do some thing on my own instead of posting here , where do i start to learn this stuff
.. really appreciate all your help..

Reading threads on this site, you can learn various ways to perform lots of different tasks. If a script is given that does something you want to do and you don't understand what the script is doing, look at the man pages on your system for the commands that you don't understand and see if you can figure out how that script works. If you can't, add a post to that thread asking for an explanation of what that command is doing.

Note that some utilities behave differently on different operating systems (and even on different releases of the same operating system). So it may be that some of the examples given will work as described on the system(s) on which they were tested, but will not work on your system.

Note that back in post #5 I asked you what operating system and shell you're using. In post #10 you finally told us that you're using bash as your shell, but you still haven't told us what operating system you're using. Giving us this information in the first post of a thread makes it much more likely that you'll get suggestions that help you do what you want to do much faster and greatly reduce the number of suggestions provided that will work on some systems, but will not work on your system. The more details you can give us about what you are trying to do, what you have tried, what is and/or is not working, will help us help you learn about what options you have to do what you're trying to do. The fewer details you give, the more wild guesses we have to make and it becomes more likely that suggestions provided won't do what you're really trying to do.

Making lots of wild guesses on what you are trying to do from what has been said in this thread, I might guess that you have a script that wants to read a file named pom.xml , find the version string in that file, update that version string and save that updated value in a variable named b and in the file pom.xml . If that is what you're trying to do, you might want to consider the following script as an alternative to some of the suggestions that have been provided so far (which only update a version number and save the updated value in a variable named b ). (Note that I generally use ksh instead of bash because I sometimes like to perform floating point calculations with arithmetic expansions, I like that the last element on a pipeline runs in the current shell execution environment, and frequently runs a little faster; but the following script will work with no changes whether it is run by bash or ksh .)

The following script takes one operand naming a file to be processed. (If no operand is supplied, it defaults to using pom.xml .) It contains an awk script that reads that file, looks for version strings, and produces two outputs. One output is a new file with the name of the input file with .new appended that contains the text from the input file with updated version strings. The other output is a list of updated version strings, one per line, written to its standard output. This script prints the contents of the input file, calls the awk script (saving the output in a variable named b ), prints the contents of that variable, and then prints the contents of the updated file produced by the awk script:

#!/bin/ksh
file=${1:-pom.xml}
printf '"%s" contains:\n' "$file"
cat "$file"
b=$(awk '
match($0, /[[]project-SNAPSHOT-([0-9][0-9][.]){3}[0-9][0-9]]/) {
	vn = substr($0, RSTART, RLENGTH)
	gsub(/[^0-9]/, "", vn)
	vn = ("1" vn) + 1
	vn = substr(vn, 2, 2) "." substr(vn, 4, 2) "." \
	     substr(vn, 6, 2) "." substr(vn, 8, 2)
	print substr($0, 1, RSTART + 17) vn \
	      substr($0, RSTART + RLENGTH - 1) > (FILENAME ".new")
	print vn
	next
}
{	print > (FILENAME ".new")
}' "$file")

printf '\n\nb has been set to "%s"\n\n"%s.new" contains:\n' "$b" "$file"
cat "$file.new"

If you want to try this script on a Solaris/SunOS system, change awk in the script to /usr/xpg4/bin/awk or nawk .

I created two sample input files, one named pom.xml containing just one version string (which I assume is what you will be doing most of the time) and one named ultiverions.xml containing several version strings making it easier to verify that the code is working as expected without creating lots of test files.

If the above script is named tester and made executable, the command:

./tester

and the command:

./tester pom.xml

produce the output:

"pom.xml" contains:
<xml>
<version>[project-SNAPSHOT-96.99.99.99]</version>
<tag>... ... ...</tag>
<more_tags>
...
...
...
</more_tags>
</xml>


b has been set to "97.00.00.00"

"pom.xml.new" contains:
<xml>
<version>[project-SNAPSHOT-97.00.00.00]</version>
<tag>... ... ...</tag>
<more_tags>
...
...
...
</more_tags>
</xml>

and the command:

./tester multiverions.xml

produces the output:

"multiverions.xml" contains:
am reading it from pom.xml, lets say original one is [project-SNAPSHOT-00.00.00.01]
And i should change that to [project-SNAPSHOT-00.00.00.02] push back git
a line with no snapshot
3rd snapshot rollover to 00.01.00.00 [project-SNAPSHOT-00.00.99.99] after
4th snapshot rollover to 99.00.00.00 [project-SNAPSHOT-98.99.99.99] after
5th snapshot rollover to 00.00.00.00 [project-SNAPSHOT-99.99.99.99] after


b has been set to "00.00.00.02
00.00.00.03
00.01.00.00
99.00.00.00
00.00.00.00"

"multiverions.xml.new" contains:
am reading it from pom.xml, lets say original one is [project-SNAPSHOT-00.00.00.02]
And i should change that to [project-SNAPSHOT-00.00.00.03] push back git
a line with no snapshot
3rd snapshot rollover to 00.01.00.00 [project-SNAPSHOT-00.01.00.00] after
4th snapshot rollover to 99.00.00.00 [project-SNAPSHOT-99.00.00.00] after
5th snapshot rollover to 00.00.00.00 [project-SNAPSHOT-00.00.00.00] after