Hex compare in shell
Hi
Can anybody show me how to compare Hex number in shell? I define A=`grep log artifact|cut -d":" -f3`,and it echos the A is 0x5a,and how can I do to compare whether it equals 0x5a or not? I tried == and -eq but both in vain. Thank you |
'==' should work - '0x5a' is not a byte, it's a string.
Code:
$ B='\x5a' |
Quote:
Code:
[ssh@localhost hctest]$ A=0x5a |
Hm, I haven't seen that before. What does
Code:
echo ${#A} |
Quote:
Code:
** Some port to listen on: |
Code:
A=`grep -iw port artifact|grep -v ^*|awk -v FS=" " '{print $3}'` Code:
A=$( awk '/Port.*REG_DWORD/ { print $3 }' ) ($(..) is highly recommended over `..`) Is it certain that there will always be only one line that matches though? If anything other than the field you want gets into the output you're not going to get a match. So when debugging code, you should start by echoing your variables, preferably with some kind of brackets around them to make any whitespace visible. Another possibility is the line ending. This looks like the output of a windows registry or somesuch, and if the file has dos-style line endings, the invisible carriage return could be ending up tacked onto the end of the field. Try running it through cat -A: Code:
echo "[$A]" | cat -A |
Quote:
|
Actually for file conversions, you can use the dos2unix & unix2dos cmds
http://linux.die.net/man/1/unix2dos |
cat -A displays non-printing ascii characters in caret notation format, except for LF (line feed,\n) line-endings, which are represented with "$". "^M" is of course the caret notation for CR (carriage return,\r).
(If you weren't aware, the dos line format uses CRLF, while unix uses LF only. awk expects unix style, which is why you end up with an extra carriage return at the end of the line.) There are any number of ways to convert between the two, from stand-alone programs like tofrodos, to simple tr, sed, or awk commands, or anything really that can process text. There's really no correct or best way. You could even do it in the shell with a simple parameter substitution. Code:
A=${A%.} #removes the final character, no matter what it is Code:
awk -v RS='\r?\n' '/Port.*REG_DWORD/ { print $3 }' |
A lot of this thread seems to arise from the misunderstanding of the distinction between the value of a byte and the representation of that value. A byte is eight ordered bits, and has the same value irrespective of how we represent it. The representation commonly reflects the context of usage, but never affects the value.
A problem arises when people use terminology like 'Hex number' which is a mixture of terms. A number is a number, and has a value. Its representation may be in many forms, of which one is hex. The problem is that expressing the representation as part of the problem implies that we must treat the data as string/character, since that is the only way to represent data in any radix-specific context. If the data is not string/character data, then expressing the radix is irrelevant. Whenever there is any question about the nature of some data, I find it useful to display the data using od. I prefer to represent the data in hex, since that tends to be context agnostic. Reperesenting it as character/text can lead to confusion, since some characters have particular meanings in some contexts, line-endings like Carriage-returns and Linfeeds in particular. od quickly and unambiguously exposes these, displaying CRs and LFs in hex as 0D and 0A, respectively. I like to use the form Code:
od -tx1 someArbitraryFile The problem gets somewhat compounded (and becomes more confounding) when some programming languages silently convert our strings that represent numbers (like here; '0x6300') into integer or floating point scalar variables. It often isn't clear which interpretation a language will make. For instance in article two of this thread, Snark1994 gives the example Code:
$ B='\x5a' Code:
$ B='\x5a' --- rod. |
the echo command in bash doesn't automatically expand hex values, or any other backslash references, by default. That's what the -e option is for. (The '%b' token in printf also expands them.)
But you can also turn expansion on globally with the xpg_echo shell option. Then you can use the -E to disable it locally. Code:
$ B='\x5a' Code:
$ B=$'\x5a' |
Quote:
Thanks for the 'od' tip, I often find it frustrating when debugging bash to work out what /exactly/ a variable contains... |
All times are GMT -5. The time now is 03:37 AM. |