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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-24-2006, 03:04 PM
|
#1
|
LQ Newbie
Registered: Jun 2004
Posts: 7
Rep:
|
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
|
|
|
01-25-2006, 08:01 AM
|
#2
|
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 12:09 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|