trouble with: read -d '\r'

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting trouble with: read -d '\r'
# 1  
Old 10-08-2009
trouble with: read -d '\r'

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.|
~>  s=$(echo $s | tr -d '\r'); echo -n $s > xf1; hexdump -C xf1;
00000000  61 62 63 64 65                                    |abcde|

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|

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

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

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

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|

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

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

# dude!

Last edited by HowardL; 10-08-2009 at 02:34 AM..
# 2  
Old 10-08-2009

read < xf1


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

# 3  
Old 10-12-2009
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}"

...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}"

# cool!

Last edited by HowardL; 10-12-2009 at 03:49 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 Replies

2. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

3. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

4. Shell Programming and Scripting

Read Embedded Newline characters with read (builtin) in KSH93

Hi Guys, Happy New Year to you all! I have a requirement to read an embedded new-line using KSH's read builtin. Here is what I am trying to do: run_sql "select guestid, address, email from guest" | while read id addr email do ## Biz logic goes here done I can take care of any... (6 Replies)
Discussion started by: a_programmer
6 Replies

5. UNIX for Advanced & Expert Users

read() wont allow me to read files larger than 2 gig (on a 64bit)

Hi the following c-code utilizing the 'read()' man 2 read method cant read in files larger that 2gig. Hi I've found a strange problem on ubuntu64bit, that limits the data you are allowed to allocate on a 64bit platform using the c function 'read()' The following program wont allow to allocate... (14 Replies)
Discussion started by: monkeyking
14 Replies

6. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

7. UNIX for Dummies Questions & Answers

trouble using read to store values in variables from command output

I know there are caveats about using read in pipelines because read is treated by a subshell. I know this but I can't think of any way to accomplish this regardless, I'm still a rookie. I hope somebody will be able to interpret what it is that I'm trying to accomplish and correct me. ... (2 Replies)
Discussion started by: ProGrammar
2 Replies

8. UNIX for Dummies Questions & Answers

X trouble

Hi there, I'm new to unix-environments. I'm richard, and i'm mostly a web-developer, under php. I've done work in unix env before, but never had my own. Today, I've got debian 3.1 r4 from the official site, and i've attempted to install it twice. I installed it initially as "Desktop... (0 Replies)
Discussion started by: izua
0 Replies

9. Solaris

Trouble with tr

I'm not sure where to post this but it's happening on a SunOS 5.8 server so I'll try here. I've discovered some unexpected behavior when using tr. For example: echo a | tr Z echo b | tr a echo a | tr B echo a | tr B echo a | tr A (8 Replies)
Discussion started by: Mike@Work
8 Replies

10. UNIX for Dummies Questions & Answers

The trouble about SU ...

Hi all, having read lots of posts about SU I don't quiet understand this : I'm doing regular backups of my database (u betta do) and therefore use su - username -c "sqlscript special data_base" in a unixscript which is even using cron. (yep!) Now I need some other script, still with this... (4 Replies)
Discussion started by: nulnul7
4 Replies
Login or Register to Ask a Question