LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash script, how to send commands and read replies from a modem (https://www.linuxquestions.org/questions/linux-general-1/bash-script-how-to-send-commands-and-read-replies-from-a-modem-407509/)

ealdaz 01-24-2006 03:04 PM

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

timmeke 01-25-2006 08:01 AM

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 04:32 AM.