LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-24-2006, 03:04 PM   #1
ealdaz
LQ Newbie
 
Registered: Jun 2004
Posts: 7

Rep: Reputation: 0
Bash script, how to send commands and read replies from a modem


Dear all

I want to send a command to my modem, and read it's reply, but I can't find an elegant way to do it.

I'm not talking about starting a ppp connection or anything like that, i've got that going fine with pppd, I just want to extract information from the modem, such as it's brand, imsi information, signal strength etc..

I have tried sending a command to the modem and reading the reply immediately after without success, like so:
#!/bin/bash
echo AT > /dev/modem
read LINE < /dev/modem
echo $LINE

I only read what I send, that is "AT" , I don't read the reply, which should be "OK". (If i include a second read, in case i'm getting an echo the script blocks as it waits to satisfy the second read, ie nothing else comes back)

The only alternative I have found that is succesful is this horrible hack, that starts a cat in the background, to store the answers of the modem in a file.

# Store the response of the modem
cat /dev/modem > /home/mds/imsi.txt &
sleep 1
echo AT > /dev/modem
sleep 2
killall cat

Any ideas of how to do it properly?
Many thanks!

Eduardo
 
Old 01-25-2006, 08:01 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
So, what you want to do is simply to catch whatever your modem writes on standard output (stdout, ie your terminal)?
To do so, use backticks. See the man page of your shell for more info.
For instance, in Bash:
my_var=`echo AT`
would give me the string "AT", outputted by "echo" back in the variable $my_var.

If that doesn't work, try putting it in a subshell, so by enclosing the command in regular braces ie ().

In your code, you first issue the "echo" command, which indeed sends the string "AT" to your modem device.
The modem responds and outputs the results (on stdout).
The stdout that is used by the modem for printing is attached to your terminal (if you are using one).
For instance, if the script was running in the background (without a terminal attached), then you wouldn't see the output, unless the program calling the "echo" command catches it.

Your "read" call however, reads from the stdin stream of your terminal (again, if you're using one).
Your modem never sends anything on the stdin stream of your terminal, only your keyboard does. So, you'll have to type in something when your program seems to be "hanging" on the read line. You can stop sending keyboard input (ie terminate the "read") by tapping Ctrl-D on your keyboard. But anyway, that won't accomplish what you want.

Backticks, however, provide a way to execute a command and capture it's output.
Subshells create a new shell (command environment), copying all your environment settings to it, run the
command you specified in that new environment and then return to your original program.
So, no matter on what stream the command outputs (stdout or stderr), you'll see it's output on the subshell's stdout.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I, remotely via LAN, connect to a modem and send it commands ? djshark Linux - Networking 1 06-06-2004 11:46 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script to send mail Cruger Programming 1 03-06-2004 08:25 AM
commands behind a bash here script onurbi Linux - Software 3 11-14-2003 07:38 AM
Using vi commands in a bash script tiger Programming 1 05-21-2001 05:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:14 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