How to read output in AGI Bash

Hi,

I’m trying find out how to read output from AGI script to bash vaariable. For example

#!/bin/bash
echo "Answer"
echo "EXEC Read WYBOR,tt-weasels,0,1"
echo "GET Variable WYBOR"
echo 'EXEC Noop ${WYBOR}'
echo 'NOOP ${WYBOR}'

How to fetch returned value from “GET Variable” to bash variable? I’m able to see that on console

<PJSIP/100-000000c3>AGI Rx << GET Variable WYBOR
<PJSIP/100-000000c3>AGI Tx >> 200 result=1 (321)

But the value “200 result=1 (321)” i wanna assign to my var.

You can only set environment variables, not local shell variables, and they will only be visible to sub-processes of the AGI script, which will not, in general, be running on a console.

If you want to set something that is globally visible on the machine, you will need to do it by manipulating files in the filesystem.

Please explain what your real goals are, at a much higher level.

I need fetch output to script variable, When I doing

echo "GET Variable WYBOR"

I get return like

200 result=1 (321)

so all what i need is to know how returned value assign to bash value, so when i echo $RESPONSE i should get this returned value

EDIT:
In short, i have to know what user provided with AGI script.

Please see http://www.catb.org/~esr/faqs/smart-questions.html#goal You still haven’t said what the real goal is.

All what I need. Capture input code by callee part.

Someone call to my system, i send him to AGI script which tell “please, provide code and press pound (#) key” and question is: how i know what code he provide in AGI?

I don’t see where BASH comes in, but is this the sort of thing you want: http://phpagi.sourceforge.net/phpagi2/docs/__examplesource/exsource__root_phpagi-2.14_examples_dtmf.php_9f0d08538805cb50bb0f290606fe78d3.html ?

Most people use PHP. This is a bash question, not an Asterisk one.

I’m not sure if you can force bash into line buffered mode, but typically you might use read, but you could also use head. There are going to be many ways of doing it, as long they can be made to operate with line buffering. You might even use sed directly. The final capture into a shell variable would involve back ticks or, with bash, $(…) syntax, combined with an assignment.

You will, of course, have to read and ignore all the other AGI output from Asterisk.

Typically one wouldn’t start the AGI until one had already done the read from the dialplan, in which case the value would be passed as a command line parameter.

Ironically, the title of your topic says exactly what you need to do. You need to ‘read’ Asterisk’s ‘output.’

While you (generally) can write an AGI in Bash, you (specifically) should not.

Your snippet shows you do not understand the AGI protocol and that you do not understand Bash sufficiently.

When Asterisk starts an AGI, it provides the ‘AGI environment’ on STDIN. You did not read this.

AGI is a request/response protocol. You ‘write’ (STDOUT) to request Asterisk to do something and then you ‘read’ (STDIN) the response. You do not do this.

Thus, you are violating the protocol. Violating the protocol leads to all sorts of weird problems.

Your best option would be to use a language with an established AGI library, like one of the ‘P’ languages or C.

Thank you guys for trying help me. After few attempts I solved my problem. I should say that there is an error in the book (Asterisk- The Definitive Guide, 4th Edition) because in the chapter that talks about AGI is sample code that does not work properly, I found what it should look like on page http://marcelog.github.io/articles/agi.html.

I was aware that after each command called I had to read the answer, but I did not know how to deal with the first lines that are at the beginning

#!/bin/bash
line='init'  
while [ "${#line}" -gt "2" ]; do  
        read line  
done  

echo "EXEC Read CHOICE,tt-weasels,0"
read
echo "GET Variable CHOICE"
read
echo $(echo ${REPLY} | awk {'print $3'} | sed 's/[\(\)]//g')
  1. Please use 'Preformatted text' when posting anything other than just plain text.

  2. This code doesn’t run. You transposed your braces and single-quotes in your awk command. You should ‘cut-n-paste’ into preformatted text instead of 'retype. ’

  3. ‘echo-ing’ the output of ‘echo’ is redundant. You could replace that line with something like:

awk '{gsub("[()]", "", $3); print $3}' <<<"${REPLY}"
  1. Generating output on STDOUT violates the AGI protocol.

  2. I still think you should invest the energy to learn one of the ‘P’ languages and use an established AGI library. It will expand what you can accomplish and save you time in the long term.

  1. done
  2. i did copy-n-paste
  3. thank you for learn
  4. its only for testing and showing its work in agi debug mode
  5. yeah, i dont think i blocked on that step so I have more complicated code and i want rewrite it in PHP or Python

The verbose command is the correct way to show output on the console.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.