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.
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?
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')
Please use 'Preformatted text' when posting anything other than just plain text.
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. ’
‘echo-ing’ the output of ‘echo’ is redundant. You could replace that line with something like:
Generating output on STDOUT violates the AGI protocol.
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.