Synopsis:
Execute a shell command and save the result as a variable.Description of application:
Backticks(<VARNAME>|<command>)<VARNAME> - The name of the variable to be set.
<command> - The shell command to execute; be sure to provide the full path to the command. I used sh, but I think any language will do.
Description of function:
Backticks(<command>)<command> - Same as above.
Notes
- *CLI> show application BACKTICKS
- *CLI> show function BACKTICKS
Return value
Returns the resulting string.Example
- application:
- function:
- command.sh:
x=0
if [ -e "/some/path/$1/caller-$2.ulaw" ]; then x=1; fi /* see man test for info on the if argument */
echo $x
The above takes two args, checks for the existence of the file, if it exists return 1, else return 0; i.e. the resulting value of foo.
Easy Install
Follow his install instructions except:- you may want to save app_backticks.c to /usr/src/asterisk
- for his step 3, cd /usr/src/asterisk first and then execute /usr/src/asterisk/contrib/scripts/astxs -install app_backticks.c
See also
9356 views strong.
Page Changes
Backticks in debian
hopefully will be included in asterisk base package.
Beware of end-of-line II
BackTicks for Asterisk 1.4
Can you put your code in a file? because this site modify the code.
I`m using asterisk 1.4 and app_backticks don`t compile. The compile error is:
make: *** No rule to make target `apps_env'. Stop.
-I/usr/src/asterisk-1.4.17/ -I/usr/src/asterisk-1.4.17//include
-c app_backticks.c -o app_backticks.o
sh: -/: invalid option
Usage: sh GNU long option option ...
Thanks!
BackTicks for Asterisk 1.4
/*
* backticks Application For Asterisk
*
* Copyright (c) 2004-2007 Anthony Minessale II <anthmct@yahoo.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*
* ported to 1.4 by Hoai-Anh Ngo-Vi <hoaianh@web.de>
*
*
*/
static char *tdesc = "backticks";
static char *app = "BackTicks";
static char *synopsis = "Execute a shell command and save the result as a variable.";
static char *desc = ""
" Backticks(<VARNAME>|<command>)\n\n"
"Be sure to include a full path!\n"
;
//STANDARD_LOCAL_USER;
//LOCAL_USER_DECL;
static char *do_backticks(char *command, char *buf, size_t len)
{
int fds2, pid = 0;
char *ret = NULL;
memset(buf, 0, len);
if (pipe(fds)) {
ast_log(LOG_WARNING, "Pipe/Exec failed\n");
} else { /* good to go*/
pid = fork();
if (pid < 0) { /* ok maybe not */
ast_log(LOG_WARNING, "Fork failed\n");
close(fds0);
close(fds1);
} else if (pid) { /* parent */
close(fds1);
read(fds0, buf, len);
close(fds0);
ret = buf;
} else { /* child */
char *argv255 = {0};
int argc = 0;
char *p;
char *mycmd = ast_strdupa(command);
close(fds0);
dup2(fds1, STDOUT_FILENO);
argvargc++ = mycmd;
do {
if p = strchr(mycmd, ' ') {
*p = '\0';
mycmd = ++p;
argvargc++ = mycmd;
}
} while (p);
close(fds1);
execv(argv0, argv);
/* DoH! */
ast_log(LOG_ERROR, "exec of %s failed\n", argv0);
exit(0);
}
}
return buf;
}
static int backticks_exec(struct ast_channel *chan, void *data)
{
int res = 0;
struct ast_module_user *u;
const char *usage = "Usage: Backticks(<VARNAME>|<command>)";
char buf1024, *argv2, *mydata;
int argc = 0;
if (!data) {
ast_log(LOG_WARNING, "%s\n", usage);
return -1;
}
u = ast_module_user_add(chan);
/* Do our thing here */
if (!(mydata = ast_strdupa(data))) {
ast_log(LOG_ERROR, "Memory Error!\n");
res = -1;
} else {
if((argc = ast_app_separate_args(mydata, '|', argv, sizeof(argv) / sizeof(argv0))) < 2) {
ast_log(LOG_WARNING, "%s\n", usage);
res = -1;
}
if (do_backticks(argv1, buf, sizeof(buf))) {
pbx_builtin_setvar_helper(chan, argv0, buf);
} else {
ast_log(LOG_WARNING, "No Data!\n");
res = -1;
}
}
ast_module_user_remove(u);
return res;
}
static char *function_backticks(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
char *ret = NULL;
if (do_backticks(data, buf, len)) {
ret = buf;
}
return ret;
}
static struct ast_custom_function backticks_function = {
.name = "BACKTICKS",
.desc = "Executes a shell command and evaluates to the result.",
.syntax = "BACKTICKS(<command>)",
.synopsis = "Executes a shell command.",
.read = function_backticks
};
int unload_module(void)
{
int res;
res = ast_custom_function_unregister(&backticks_function);
res |= ast_unregister_application(app);
ast_module_user_hangup_all();
return res;
}
int load_module(void)
{
int res;
res = ast_custom_function_register(&backticks_function);
res |= ast_register_application(app, backticks_exec, synopsis, desc);
return res;
}
char *description(void)
{
return tdesc;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Third Party BackTicks() application");
Beware of end-of-line