LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2010, 05:00 PM   #1
vouser
LQ Newbie
 
Registered: Mar 2010
Posts: 10

Rep: Reputation: 0
Writing to and reading from a socket from bash script.


Hello all.
Here the description of the issue I am having.
I am writing a bash test script which reads lines from a file, builds ISO messages, sends them to a server,
reads the response with response code and reports the result of the test to a file or on the screen.

The message that I need to send is 94 characters long.
Here's the portion of a code that I initially wrote:

###############################################

#~ Open socket.
exec 3<>/dev/tcp/172.26.0.25/9991

#~ Send msg.
echo "$msg_out" >&3 # OK but adds EOL character which confuses the server.


#~ Receive msg.
read -r msg_in <&3
echo "msg_in: $msg_in"

###############################################

It works OK with the "echoserver".
However, it does not on a real server.
The symptom is that the program is blocked on "read" statement.
One of the facts that is confirmed by server people is that the message received by a server is actually 95 bytes long.

So, based on this information I have 2 questions:

1. Is there any other way to do what I do in line "echo "$msg_out" >&3" to avoid End Of Line character?
2. Am I using the correct way to read from socket? "read -r msg_in <&3".

Thanks for your help,
vouser.
 
Old 03-11-2010, 05:09 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Never tried using your method of using sockets in a shell script. When confronted by such tasks, I typically turn to netcat (nc, on most installations). Perhaps your newline is coming from the echo command, and can be supressed with the -n switch.
Code:
echo -n "$msg_out" >&3
--- rod.

Last edited by theNbomr; 03-11-2010 at 05:11 PM.
 
Old 03-11-2010, 05:58 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
1. Use printf(1), not echo.
2. Use the -u argument to read.
 
Old 03-11-2010, 06:30 PM   #4
vouser
LQ Newbie
 
Registered: Mar 2010
Posts: 10

Original Poster
Rep: Reputation: 0
Rod:
thanks a lot for your post.
It took care of my question #1.
With "-n" switch the server people confirmed that they get exactly 94 bytes.
I still have a problem reading from the server, which is my question #2.
What's interesting is that with "-n" switch even "echoserver" can't read the msg back (it could before).
Looks like I need a better way to read.
Thanks again,
vouser.
 
Old 03-11-2010, 08:11 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
You need -n for read as well, otherwise it waits for a newline.
 
Old 06-08-2010, 06:37 PM   #6
x29a
LQ Newbie
 
Registered: Jun 2010
Posts: 2

Rep: Reputation: 0
just in case anybody was wondering, i ended up using dd:

Code:
RETURN=`dd bs=$1 count=1 <&5 2> /dev/null`
with $1 beeing the amount of bytes to read. this wont block while reading the result.

the complete script can be found here: http://blog.chris007.de/?p=238
 
Old 05-10-2012, 05:13 PM   #7
shearerg
LQ Newbie
 
Registered: May 2012
Posts: 3

Rep: Reputation: Disabled
Hi all,

I found that the method outlined at the top of this post works fine ... as long as you use printf instead of echo, as recommended by 'tuxdev'. I've also found this in other non-socket related scripts. Result is quite an elegant solution ... as long as a blocked read is ok.

Greg
 
  


Reply

Tags
bash, native, network, sockets



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing to and reading from a socket from bash script. vouser Linux - Networking 2 03-12-2010 10:07 PM
Need help writing a bash script stympman Linux - Newbie 2 10-18-2008 07:55 PM
Reading and writing a file with bash XeroXer Linux - Newbie 7 06-02-2008 08:08 AM
bash: create a descriptor reading/writing from/to different files? Thinking Programming 0 04-19-2006 03:28 PM
BASH script – reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration