Reference AGI argument in shell script

When using AGI, how do I reference an argument passed from Asterisk to the AGI in a shell script?
I read that in PHP it’s supposed to be $ARGV[0], but what’s the way in a shell script?

exten => s,1,AGI(,${EXTEN})
 same => n,Noop(${TEST})


This is a Posix question, not an Asterisk one.

For bash, it is $1 or "$1" depending on whether or not white space should end hte value.

I would have expected the PHP to be $ARGV[1]!

1 Like

What about the standard variables that Asterisk passes to the AGI script automatically, how do I reference those in a shell script?

<SIP/8700-0000023f>AGI Tx >> agi_request:
<SIP/8700-0000023f>AGI Tx >> agi_channel: SIP/8700-0000023f
<SIP/8700-0000023f>AGI Tx >> agi_language: en
<SIP/8700-0000023f>AGI Tx >> agi_type: SIP
<SIP/8700-0000023f>AGI Tx >> agi_uniqueid: 1569707314.1541
<SIP/8700-0000023f>AGI Tx >> agi_version: 13.23.1
<SIP/8700-0000023f>AGI Tx >> agi_callerid: 8700
<SIP/8700-0000023f>AGI Tx >> agi_calleridname: name
<SIP/8700-0000023f>AGI Tx >> agi_callingpres: 0
<SIP/8700-0000023f>AGI Tx >> agi_callingani2: 0
<SIP/8700-0000023f>AGI Tx >> agi_callington: 0
<SIP/8700-0000023f>AGI Tx >> agi_callingtns: 0
<SIP/8700-0000023f>AGI Tx >> agi_dnid: 8881
<SIP/8700-0000023f>AGI Tx >> agi_rdnis: unknown
<SIP/8700-0000023f>AGI Tx >> agi_context: massalert-gosub
<SIP/8700-0000023f>AGI Tx >> agi_extension: s
<SIP/8700-0000023f>AGI Tx >> agi_priority: 1
<SIP/8700-0000023f>AGI Tx >> agi_enhanced: 0.0
<SIP/8700-0000023f>AGI Tx >> agi_accountcode:
<SIP/8700-0000023f>AGI Tx >> agi_threadid: 139904872728320
<SIP/8700-0000023f>AGI Tx >> agi_arg_1: Fire

They are presented on standard in, so any of the many ways of reading and parsing that. In bash, type “help read” for one way. However, the devil will be in the detail and that goes well beyond what can be reasonably covered in a forum reply.

Your questions imply that you don’t understand the AGI protocol yet.

Specifically, you should read the AGI environment from STDIN first. Next, for EACH request you issue to Asterisk on STDOUT, you need to read the response from STDIN. Failing to do this breaks the protocol and will result in ‘unexpected behavior.’

The protocol is simple, but subtle. Nobody gets it right the first time. I suggest you use an existing library in a language like C, Perl, PHP, etc. where you have access to debugging facilities.

This is not to denigrate shell as a programming language. Some of my best friends use shell :slight_smile: – but not for AGIs.

I wrote an AGI in FORTRAN just to prove a point, but I suggest that you should choose a tool more appropriate to the task.

1 Like

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