Unix Shell - Discrepancy while reading and comparing binary files

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2007 > Discrepancy while reading and comparing binary files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Discrepancy while reading and comparing binary files
Madhusudhanan Chandrasekaran

2007-05-28, 7:19 pm

Hi,

I have a script readbinary.sh with the following three lines

#!/bin/bash
var=`cat binaryfile`
echo $var

but if I run the file readbinary.sh > trial and do a diff

vimdiff trial binaryfile they are not the same. Moreover the
size of the two files are not the same.This is the case even
if I modify the script to read line by line. This prompts me
to think that shell variables have some pre-specified size.

Also, the discrepancy in the start of the two files binaryfile
and trial is even more baffling.

Please advise where I might be going wrong.

Thanks in advance
_Madhu
Barry Margolin

2007-05-28, 7:19 pm

In article <f3fmt1$jrd$1@prometheus.acsu.buffalo.edu>,
Madhusudhanan Chandrasekaran <mc79@cse.buffalo.edu> wrote:

> Hi,
>
> I have a script readbinary.sh with the following three lines
>
> #!/bin/bash
> var=`cat binaryfile`
> echo $var
>
> but if I run the file readbinary.sh > trial and do a diff
>
> vimdiff trial binaryfile they are not the same. Moreover the
> size of the two files are not the same.This is the case even
> if I modify the script to read line by line. This prompts me
> to think that shell variables have some pre-specified size.
>
> Also, the discrepancy in the start of the two files binaryfile
> and trial is even more baffling.
>
> Please advise where I might be going wrong.
>
> Thanks in advance
> _Madhu


Since you didn't quote "$var", the shell will perform word splitting
(essentially replacing all runs of whitespace with a single space) and
wildcard processing.

Also, if there are any NUL characters in the file, they'll be discarded
by the shell.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com