trouble with: read -d '\r'

Hello,
I am writing a script to talk to my pop server.
All the lines returned from the server end with \r\n (0x0d 0x0a)
The read command trims the 0x0a and the line is left with 0x0d ending.
I see how I can use the tr utility do delete it:

~>  s=abcdex; s=$(echo $s | tr x '\r'); echo -n $s > xf1; hexdump -C xf1;
00000000  61 62 63 64 65 0d                                 |abcde.|
00000006
~>  s=$(echo $s | tr -d '\r'); echo -n $s > xf1; hexdump -C xf1;
00000000  61 62 63 64 65                                    |abcde|
00000005

Which is nice , but it would be more efficient to just stop reading when \r is encountered.
So I have been trying to figure out how to get read to see the \r.

~>  s=abcdexyz; s=$(echo $s | tr x '\r'); echo -n $s > xf1; hexdump -C xf1;
00000000  61 62 63 64 65 0d 79 7a                           |abcde.yz|
00000008

# So there is an 0x0d , Now use read -d on a regular character:

~>  read -d e < "xf1"; echo "$REPLY"
abcd

# But I can't seem to figure out how to get read to id the 0x0d:
 
~>  read -d \r < "xf1"; echo "$REPLY"
yzcde
~>  read -d '\r' < "xf1"; echo "$REPLY"
yzcde
~>  read -d '\\r' < "xf1"; echo "$REPLY"
yzcde
~>  read -d \\r < "xf1"; echo "$REPLY"
yzcde
~>  read -d 0x0d < "xf1"; echo "$REPLY"
yzcde

How can this be done?

---------- Post updated 10-08-09 at 01:29 AM ---------- Previous update was 10-07-09 at 11:32 PM ----------

I'll be dog , I think I found an answer in ABS:

~>  volume="hi"$'\x0d'" there";  echo -n $volume > xfile;  hexdump -C xfile;
00000000  68 69 0d 20 74 68 65 72  65                       |hi. there|
00000009

~>   read -d '\r' < xfile; echo "$REPLY"
 there

~>   read -d $'\x0d' < xfile; echo "$REPLY"
hi

# dude!
read < xf1
REPLY=${REPLY%$'\r'}

Or:

IFS=$' \t\r\n' read < xf1

Thanks for the reply.
I am comfortable with the first method you posted.

But I can't figure out how the second is supposed to work.

IFS=$' \t\r\n' read < xf1

# ...for example I tried thing like this:
~>  s="woxrd";  echo "${s/x/$'\r'}" > xf1;  hexdump -C xf1;
00000000  77 6f 0d 72 64 0a                                 |wo.rd.|

~>  IFS= ; REPLY= ;  IFS=$' \t\r\n' read < xf1

~>  echo  -e "${IFS}" > xf2

~>  hexdump -C xf2
00000000  0a                                                |.|

~>  echo "${REPLY}"
rd

...and I don't get what I'm supposed to be doing with that... Is IFS some special variable?

I also wondered if it was possible to assign a \r right into a string:

~>  echo "String of $'\x0d' words"
String of $'\x0d' words

~>  echo "String of $'\r' words"
String of $'\r' words

There sure are a lot of in's and out's with shell scripting aren't there.
Thanks, Howard();

---------- Post updated at 02:37 AM ---------- Previous update was at 01:52 AM ----------

Ah! I see a little more now as far as assignment goes:

~>  s=$'abcde\a\t\rfghi'

~>  echo -e "${s}" > xf2

~>  hexdump -C xf2
00000000  61 62 63 64 65 07 09 0d  66 67 68 69 0a           |abcde...fghi.|

~>  echo -e "${s}"
fghie

# cool!