GIDForums  

Go Back   GIDForums > Computer Forums > Computer Software Forum - Linux
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Sep-2009, 20:50
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Bash test for a dot terminatior in pop3 server LIST output


Yes , an 0x2e dot.
I've been writing a script to talk to my pop email sever.
Here is an actual short manual session of what i'm trying to script so far:
Code:
~> telnet mail.blahblah.org 110 Trying 192.94.73.19... Connected to mail.blahblah.org (192.74.51.10). Escape character is '^]'. +OK Teapop [0.3.8] - Teaspoon stirs around again <1254161111.670F3C71@Llywellyn> USER <my_email_username> +OK Welcome, do you have any type of ID? PASS <my_email_password> +OK I'm ready to serve you, Master. STAT +OK 11 30687 LIST +OK These are my measures. 1 686 2 657 3 1481 4 619 5 2410 6 5951 7 1932 8 6117 9 2227 10 4102 11 4505 . QUIT +OK It has been a pleasure serving you. Connection closed by foreign host. ~>
I used this reference to learn those commands: :faqs.org/rfcs/rfc1939.html
I manually enter all the commands - USER , PASS etc.

Ok , so you can see there that after the LIST command is issued I
get a list of two-"word" strings each on a separate line terminated
by a single dot on a line.

In my script I use a while loop to 'read' each line that is output by LIST.
The condition of the while loop is that damn dot.

I can catch a dot I 'read' from my keyboard
but when I try it on the output from the server it's not caught.
I don't know if it's unicode or what but I'm stumped.

Here is a demo of catching the dot with me doing the input:
CPP / C++ / C Code:
#!/bin/sh
 
echo -e "----------\n\
Simulate pop server LIST output by entering <Msg# Size> strings.\n\
Terminate by typing a single . . eg:\n1 345\n2 654\n.\n";
 
while [ "$REPLY" != . ]; do     # a string containing only  dot (0x2e) 
 
  # read automatically assigns input to $REPLY unless another's specified
  read ;     # read tmp  will get the string into $tmp  
 
  msg=( $REPLY );  # this parses the string into "word" elements
 
  echo  "${REPLY[*]} , ${msg[0]} , ${msg[1]} , ${msg[2]}";
 
done;
 
echo -e "----------\n\
And if you could do a couple more, please:\n.\n";
 
msg="";
while [ "${msg[0]}" != . ] ;  do   # a word containing only one dot (0x2e)
 
  read   -a msg         # the -a automatically parses input into "word" elements
 
  echo  "${msg[*]} , ${msg[0]} , ${msg[1]} , ${msg[2]}";
 
  if [ "${msg[0]}" == . ]; then
    echo "Got dot";
  fi
 
done
 
echo "Loop done ...goodbye.";
And here is the script for the server where I'm not catching the dot.

When I run it the server list output stops , the dot is not detected
so the loop continues and I end up hung on a read and must use
ctrl-c to stop the process.
CPP / C++ / C Code:
 #!/bin/sh
server="mail.blahblah.org";   user="MYUSERNAME"; pass="MYPASSWORD";
 
exec  3<>/dev/tcp/$server/pop3 || exit 1;  # assign a tcp to port 110 connection
                                           # to fd 3
read  -u 3 ;               # read out the greeting (msg gets last line)
echo  "$REPLY";            # print the message to the screen
 
echo  "USER $user" 1>&3;   # send username to the server 
read  -u 3 ;               # read out the response into msg
echo  "$REPLY";            # print message to screen
 
echo  "PASS $pass" 1>&3;   # send password
read  -u 3 ;               # read out the response into msg
echo  "$REPLY";            # print message to screen
 
echo  "STAT";
echo  "STAT" 1>&3;         # stat show number of messages and total size
read  -u 3 ;
echo  "$REPLY";
 
echo  "LIST";
echo  "LIST" 1>&3;         # LIST shows each message and it's size 
read  -u 3  ;              # get and print the OK line
echo  "$REPLY";
 
 
# loop through the LISTings
 
while [ "$REPLY" != . ]; do
  
  read  -u 3  ;
 
  if [ "$REPLY" == . ]; then
    echo "Got dot";
  fi
  
  echo  "$REPLY";
 
done;
 
echo "LOOP DONE";
 
echo "quiting"
echo   "QUIT" 1>&3
read   -u 3
echo   $REPLY
I got some of the ideas use in the above from: linuxgazette.net/110/park1.html
Obviously I am not too experienced with bash...
HELP! , thanks, Howard();
  #2  
Old 29-Sep-2009, 01:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: bash test for a dot terminatior in pop3 server LIST output


Quote:
Originally Posted by Howard_L
CPP / C++ / C Code:
.
.
# loop through the LISTings
 
while [ "$REPLY" != . ]; do
  
  read -u 3  ;
 
  if [ "$REPLY" == . ]; then
    echo "Got dot";
  fi
  
  echo  "$REPLY";
 
done;
 

What do you see if you change the loop output messages to be a little more informative:
Code:
. . . # loop through the LISTings while [ "$REPLY" != "." ] ; do read -u 3 echo -n "REPLY = --->$REPLY<---" echo "Stuff after Reply" if [ "$REPLY" == "." ] ; then echo "Got dot" fi done echo "LOOP DONE"

Looking at your logic, if the last message is simply a dot, I would expect to see something like

Code:
REPLY = --->1 686<---Stuff after Reply REPLY = --->2 657<---Stuff after Reply REPLY = --->3 1481<---Stuff after Reply REPLY = --->4 619<---Stuff after Reply REPLY = --->5 2410<---Stuff after Reply REPLY = --->6 5951<---Stuff after Reply REPLY = --->7 1932<---Stuff after Reply REPLY = --->8 6117<---Stuff after Reply REPLY = --->9 2227<---Stuff after Reply REPLY = --->10 4102<---Stuff after Reply REPLY = --->11 4505<---Stuff after Reply REPLY = --->.<---Stuff after Reply Got dot LOOP DONE

Since your script never showed "Got dot", maybe there is some extra character on the line after the dot (A '\r' comes to mind; maybe some other whitespace.) By surrounding the response with ---><--- you can (maybe) see if there is some extra little "gift." So, for example, if the server is sending "\r\n" as line termination characters you might see something like
Code:
<---Stuff after Reply <---Stuff after Reply <---Stuff after Reply <---Stuff after Reply <---Stuff after Reply <---Stuff after Reply <---Stuff after Reply <---Stuff after Reply <---Stuff after Reply <---Stuff after Reply <---Stuff after Reply <---Stuff after Reply

If that's the case, then try something like the following for your loop:
Code:
#!/bin/bash . . . # loop through the LISTings FIRST=${REPLY:0:1} while [ "${FIRST}" != "." ] ; do read -u 3 FIRST=${REPLY:0:1} echo ${REPLY} if [ "${FIRST}" = "." ] ; then echo "Got dot" fi done echo "LOOP DONE"



(This should work whether there is extra stuff after the dot or not.)

Regards,

Dave
Last edited by davekw7x : 29-Sep-2009 at 02:55.
  #3  
Old 29-Sep-2009, 09:47
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Bash test for a dot terminatior in pop3 server LIST output


CPP / C++ / C Code:
${REPLY:0:1}
Wow now that's a different dereferencer! So you CAN get down to the chars?
No offense to Mr. Bourne et al but all these bash nuances are mind boggling...

You were right! There is a \r\n line ending.
The read command strips the \n.
I added a line to write each string to a file:
CPP / C++ / C Code:
echo  -n "$REPLY" >> xfile;  # -n supresses echo's default of adding a newline
...and check the file with hexdump:
Code:
... LIST stat=+OK :These are my measures. 1 686 2 657 3 1481 4 619 5 1932 6 2884 ~> hexdump -C xfile 00000000 31 20 36 38 36 0d 32 20 36 35 37 0d 33 20 31 34 |1 686.2 657.3 14| 00000010 38 31 0d 34 20 36 31 39 0d 35 20 31 39 33 32 0d |81.4 619.5 1932.| 00000020 36 20 32 38 38 34 0d 2e 0d |6 2884...| 00000029
...you can see the 0x0d line ends.
hmm, the fact that there is no 0x0a in each $REPLY string may be why I can't get it to test as expected.
Code:
while [ "$REPLY" != . ]; do # is . 2e 0a ??? while [ "$REPLY" != "." ]; do # is . 2e 0a ??? while [ "$REPLY" != .\r ]; do # is .\r 2e 0d 0a ??? while [ "$REPLY" != ".\r" ]; do # is .\r 2e 0d 0a ??? while [ "$REPLY" != "\.\r" ]; do # who knows what that might evaluate to... ...anyhow none of them are 0e 0d which is what the last $REPLY is in hexdump. (btw, so where are the bash code tags Luciwiz?)
hmmm so last night I had resorted to getting the number of messages
and using a for loop. That turned out to be a "beginning math evaluation" learning session.
I think the for statement could be better but... it works for now.
I have now added a test for your ${REPLY:0:1} and it tests true at the dot.
So here is program as it stands now:
Code:
#!/bin/sh server="mail.blahblah.org"; user="MYUSERNAME"; pass="MYPASSWORD"; exec 3<>/dev/tcp/$server/pop3 || exit 1; # assign a tcp to port 110 connection # to fd 3 read -u 3 ; # read out the greeting (msg gets last line) echo "$REPLY"; # print the message to the screen echo "USER $user" 1>&3; # send username to the server read -u 3 ; # read out the response into msg echo "$REPLY"; # print message to screen echo "PASS $pass" 1>&3; # send password read -u 3 ; # read out the response into msg echo "$REPLY"; # print message to screen echo "STAT"; echo "STAT" 1>&3; # stat show number of messages and total size read -u 3 stat num size ; # get the "words" into separate variables (nifty) echo " stat=$stat num=$num size=$size "; echo "LIST"; echo "LIST" 1>&3; # LIST shows each message and it's size read -u 3 stat trash ; # get and print the OK line echo " stat=$stat :$trash"; if [ "$stat" != +OK ]; then echo "LIST failed"; else # loop through the LISTings # while [ "$REPLY" != . ]; do # while [ "$REPLY" != "." ]; do # while [ "$REPLY" != .\r ]; do # while [ "$REPLY" != ".\r" ]; do # while [ "$REPLY" != "\.\r" ]; do i=0; echo -n "" | cat > xfile; # start a fresh file # -n supresses echo's \n line ending while [ $i -le $num ]; do read -u 3 ; echo -n "$REPLY" >> xfile; # copy LIST output to file for hexdump FIRST=${REPLY:0:1} if [ "${FIRST}" = "." ] ; then echo "FIRST Got dot" fi if [ "$REPLY" == "\.\r" ]; then # well, I still can't get this echo "Got dot"; fi echo "$REPLY"; ((i++)) ; # increment, (()) = "do math operation" done; fi; echo "LOOP DONE"; echo "quiting" echo "QUIT" 1>&3 read -u 3 echo $REPLY
...and here is the output
Code:
~> ./popscript-3 +OK Teapop [0.3.8] - Teaspoon stirs around again <1254233090.1AFA9D1D@Llywellyn> +OK Welcome, do you have any type of ID? +OK I'm ready to serve you, Master. STAT stat=+OK num=7 size=12823 LIST stat=+OK :These are my measures. 1 686 2 657 3 1481 4 619 5 1932 6 2884 7 4564 FIRST Got dot . LOOP DONE quiting +OK It has been a pleasure serving you. ~>
I gotta go drive nails now but I am anxious to play with that new
indexing (I hate to say the word anymore) method later tonight...
Thanks for the help Dave. back later.
Howard();
  #4  
Old 29-Sep-2009, 10:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Bash test for a dot terminatior in pop3 server LIST output


Quote:
Originally Posted by Howard_L
CPP / C++ / C Code:
${REPLY:0:1}
Wow now that's a different dereferencer! So you CAN get down to the chars?
No offense to Mr. Bourne et al but all these bash nuances are mind boggling...

You can't do that with Mr Bourne's original shell (traditionally named sh). That is one of the many useful features that was added to bash. (That's born-again sh---or is it bourne-again sh?)

Note that for some Linux distributions (RedHat/Centos, for example), /bin/sh is a symbolic link to /bin/bash, so a shell script with #!/bin/sh as its first line is actually runs in a bash shell.

With other distributions (Ubuntu, as I recall), /bin/sh is a shell that is very close to the original Bourne shell and doesn't have the Bash additions, so the first line of a script that uses bash-isms should be #!/bin/bash, not #!/bin/sh.

Since many (most?) Linux distributions have bash as the default shell, you may not need anything in particular in the first line of a script unless you have changed the default to some other shell. (Some people like the Korn shell, ksh, for example, and ksh doesn't have this notation to address characters within a string.)

A final note about variables in shell scripts: Old-timers who learned sh (maybe some not-so-old timers who learned from tutorials written by or copied from old-timers who learned sh) tend to use $xyz everywhere to get the value of a shell variable. This notation is still valid, but you can always use ${xyz} instead. The reverse is not true: There are a number of things that can be done with ${xyz} that do not compute if you try to use $xyz Some tutorials go into this in some detail, including examples where the newer notation works and the older wouldn't be possible (or in some cases could be used but would be more awkward.) Accessing substrings with the ${xyz:} notation is one example for which no such operation defined using the older notation.

Regards,

Dave
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Str_Misaligned in Double Link List Peter_APIIT C Programming Language 1 29-Feb-2008 21:50
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 03:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:42.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.