1

I'm going to be retrieving two numbers as strings into variables A and B:

A will be in the form:

"10" or "0x0A" or "0x0a"

In other words, A will be either a decimal or hexadecimal number (with 0x prefix).

B will be in the form:

"image-000A" or "image-000a"

B will always be "image-" followed by 4 hexadecimal digits.

I need to compare the two numeric values and test if B > A. How do I do that in a shell script?

1 Answer 1

2

You can convert a hex number to decimal simply by multiplying it by 1.

rojo@pico:~$ t=0x0a
rojo@pico:~$ echo $t
0x0a
rojo@pico:~$ echo $(( $t * 1 ))
10

Scraping the hex value from the image is slightly more complicated, but still works the same way.

rojo@pico:~$ t=image-000a
rojo@pico:~$ echo $t
image-000a
rojo@pico:~$ echo $(( 0x${t##image-} * 1 ))
10

The ${t##image-} strips image- out of $t, leaving 000a.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.