I am attempting to compare two strings containing hexadecimal numbers in a dash test, one which has leading zeros and one which doesn't:
hex_string_1: 0x02a0000b
hex_string_2: 0x2a0000b
Trying if [ ${hex_string_1} -eq ${hex_string_2} ] ; then ...
(or vice versa, the leading zeros don't seem to matter) gives me this error:
[: Illegal number: 0x02a0000b
Is there a simple way to compare these as numbers, rather than e.g. using sed
to reformat the strings?
Since I'm using dash
and not bash
,[[
isn't an option.
UPDATE: This worked (thanks ott!):
[ $(printf "%d" ${hex_string_1}) -eq $(printf "%d" ${hex_string_2}) ]
-eq
is only allowed for for what the shell accepts as an integer. Convert the values with `iv1=$(printf "%d\n" 0x...), then compare. – ott-- yesterday