help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Reading and handling "control" characters from a file


From: Conrad J. Sabatier
Subject: [Help-bash] Reading and handling "control" characters from a file
Date: Thu, 19 Apr 2012 22:11:58 +0000 (UTC)
User-agent: Pan/0.135 (Tomorrow I'll Wake Up and Scald Myself with Tea; GIT 30dc37b master)

I've just started doing a little prototyping in bash for a program I'll 
eventually code most likely in C, and have hit a serious stumbling block 
re: the handling of characters (bytes) in the very low range of the ASCII 
table.

In a nutshell, I'm trying to read and parse the ID3 tag on an MP3 file.
Barely out of the gate, I'm running smack into the second field in the 10-
byte ID3 tag header -- bytes 3 and 4, to be precise (counting from 0) -- 
which contain the ID3 major version and revision numbers, one byte each.

The data which starts an ID3 tag header is arranged as:

bytes 0-2: the literal string "ID3"
bytes 3-4: the ID3 major version and revision numbers

Using "read -r -n 1 id3_version", bash complains loudly upon attempting 
to use the value just read (id3_version == 4):

$ id3parse.sh "Supertramp - Breakfast In America - 02 - The Logical 
Song.mp3"

[no problems with the first three bytes]
IDstr=ID3

[but then, after "read -r -n 1 id3_version", the following statement, 
which attempts to convert the value to a printable numeric character, 
causes the following]

/home/conrads/bin/id3parse.sh: line 124: : syntax error: operand 
expected (error token is "")

I realize that I am, in effect, trying to read a CTRL-D, otherwise known 
as end-of-file, so it's not too surprising that bash would get its 
panties all in a wad over this, but how to work around it?  For that 
matter, how to read *any* of the lesser-valued ASCII characters and use 
them without generating an error?

Here's the snippet of code where this is occurring:

function read_tag_header
#
# read the initial 10-byte id3v2 tag header
#
{
        # look for the initial ID string
        read -n 3 IDstr
        
        if ((IDstr != "ID3"))
        then
                echo "ID3 header not found!" >&2
                return 1
        fi

        ((DEBUG)) && echo "IDstr=${IDstr}" >&2
        
        # OK, now get the version number
        read -r -n 1 id3_version ||
        {
                echo "ID3 version number not found!" >&2
                return 1
        }

        id3_version=$((id3_version + 48))
        
It is this last expression that triggers the error.  I've tried both 
explicitly declaring id3_version as an integer type, and leaving its type 
undefined.  The expression fails the same either way.

I've wracked my brain trying to figure out a way to deal with this, and 
am just plain stumped.  Any suggestions will be much appreciated!

-- 
Conrad J. Sabatier
address@hidden




reply via email to

[Prev in Thread] Current Thread [Next in Thread]