I am trying to learn shell script and writing a simple script to increment Hex values in the loop.
Here is my script:
increment=0x0001
handle=0x0001
for((i=1;i<=20;i++))
do
echo $handle
handle=$(($handle + $increment))
handle=$(printf '%x' $handle)
done
Here is my output:
0x0001
2
3
4
5
6
7
8
9
a
1
2
3
4
5
6
7
8
9
a
It is working fine till 10th iteration but after that it is starting from 1 again.
Can any one let me know my mistake?
EDIT: After removing handle=$(printf '%x' $handle)
line output is:
0x0001
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Actually I want output in HEX only.
It has to do with how you print the value try
printf '%#x'
orprintf '%#X'
Just change the line you are using to print the content with a leading
0x
as:-(or) to have leading hex-character as
0X
With the changes, you get the output as below:-
For more formatting options check here:- http://wiki.bash-hackers.org/commands/builtin/printf (and) http://ss64.com/bash/printf.html