Exec Two Commands With ExecIf

Home » Asterisk Users » Exec Two Commands With ExecIf
Asterisk Users 10 Comments

Hi,

I didn’t see any documentation for this so I assume it can’t be done but I
figured I would check here first. Is there any way of using ExecIf to run two commands instead of 1? e.g. instead of

Exten 123,1,ExecIf($[“FOO” == “BAR”]?BackGround(you-owe))
Exten 123,1,ExecIf($[“FOO” == “BAR”]?SayNUmber(1000000″))

I would ideally like to do it in one line.

TIA.

Dovid

10 thoughts on - Exec Two Commands With ExecIf

  • Sounds like you might benefit from the If/EndIf applications: https://gerrit.asterisk.org/c/asterisk/+/16121
    You can specify a condition once, but execute multiple dialplan applications. AEL not needed. No need to branch, especially Gosub which has a huge overhead. This is probably the cleanest way to do it in dialplan, as you don’t end up with branching everywhere just to have a conditional execute 2 lines.

    exten => 123,1,If($[“FOO”=”BAR”])
    same => n,BackGround(you-owe)
    same => n,SayNumber(1000000)
    same => n,EndIf()
    same => n,Playback(goodbye)
    same => n,Hangup()

  • The dialplan is a very complicated config file. It is often repetitive and ugly. Don’t expect it to work like an actual programming language.
    If you want that, use an AGI.

    In this case, just call it an “Asterisk-ism” and move on. You’ll find plenty more of them.


    http://help.nyigc.net/

  • Assuming that the dates in that ticket refer to 2021 (I see only months and days, not years), I don’t recall any discussion taking place with the community about it, so that seems to me like a surprising explanation.

    After all, if GotoIf(), ExecIf() and While() are “deemed to be of interest”, and therefore exist, why would a simple If() be deemed not to be of interest?

    And, taking it from the other point of view, even if many people genuinely think “meh, I don’t think I’d use this”, then surely they just avoid using it, as I suspect the majority of people do with DumpChan() (for example, to take a pretty obscure, yet still available, command at random).

    In short, what’s the drawback to making If() available for those who would use it?

    Personally, I would very much like to see an If() statement made available.

    Antony.


    Tinned food was developed for the British Navy in 1813.

    The tin opener was not invented until 1858.

    Please reply to the list;
    please *don’t* CC me.

  • Thanks, Anthony. I use If() a lot in my dialplan and do find it useful as well. Personally, I agree entirely with you. However, that seemed to be the assessment at the time and I didn’t want to rock the boat too much. Along the same vein, ReturnIf() [2] is another abandoned conditional branch application that I also use quite a bit.

    Given the renewed interest, I have unabandoned the Gerrit review[1] for this so that it appears on the review queue again. Still, this doesn’t necessarily mean it will make it in.

    [1] https://gerrit.asterisk.org/c/asterisk/+/16121
    [2] https://issues.asterisk.org/jira/browse/ASTERISK-29493

  • Not really – GotoIf does generic branching, not execution. The closest thing is just using ExecIf twice. There are four general ways to do this in the dialplan that I can think of, each with their own pros and cons:

    Option 1: ExecIf, fewest lines of dialplan, but lots of repetition Same => n,ExecIf(${condition1}?App1(something))
    Same => n,ExecIf(${condition1}?App2(somethingelse))
    Same => n,ExecIf(${condition2}?App3(something))
    Same => n,ExecIf(${condition2}?App4(somethingelse))

    Option 2: If/EndIf, most “DRY” (don’t repeat yourself), probably easiest to read/maintain Same => n,If(${condition1})
    Same => n,App1(something)
    Same => n,App2(somethingelse)
    Same => n,EndIf()
    Same => n,If(${condition2})
    Same => n,App3(something)
    Same => n,App4(somethingelse)
    Same => n,EndIf()

    Option 3: using GotoIf, hardest to read, maintain, and update Same => n,GotoIf(${condition1}?:postcond1)
    Same => n,App1(something)
    Same => n,App2(somethingelse)
    Same => n(postcond1),GotoIf(${condition2}?: postcond2)
    Same => n,App3(something)
    Same => n,App4(somethingelse)
    Same => n(postcond2),NoOp()

    Option 4: using GosubIf, also easy to read, but worst performance penalty, requires additional extensions/contexts, plus might be silly for just a few lines Same => n,GosubIf(${condition1}?condition1,1)
    Same => n,GosubIf(${condition2}?condition2,1)
    Exten => condition1,App1(something))
    Same => n,App2(somethingelse)
    Same => n,Return()
    Exten => condition2,1,App3(something)
    Same => n,App4(somethingelse)
    Same => n,Return()

    Using GotoIf is probably the ugliest way to do something like this. If, ExecIf, even Gosub are arguably more elegant. Personally, I started off doing Option 3 (GotoIf) in my dialplan, moved to using Option 1 (ExecIf), and now I use Option 2 (If). I did this mainly because each progression made my dialplan easier to read and maintain. I don’t use Gosub for code that would make sense to put in a function a regular procedural programming language (like C), and If/EndIf for stuff that would typically be done “inline”, without a function call, particularly if only appears once in the whole dialplan. Often which paradigm is best depends on the context and goal.

  • Goto() and GotoIf() always remind me of programming in BASIC in the 1980s.

    Antony.


    “In fact I wanted to be John Cleese and it took me some time to realise that the job was already taken.”

    – Douglas Adams

    Please reply to the list;

  • Standard coding practices tend do discourage gotos which result in a kind of confusing spaghetti, but after years of doing this I realize telephone calls are not like typical programs and a caller is creating a thread of spaghetti woven through the telco network & pbx… which exists until you terminate the call. The answer is that you can do nearly anything within just a standard extensions.conf with a fairly limited set of conditional and loop applications – and if you can’t, there are a ton of options odbc, agi, ael…

    Antony has a point with gotos reminding me of BASIC. I compensate with an abundance of hangups. The startling difference is that asterisk dialplan instructions can work remarkably well – where basic, not so much (That was Gates’ great coding achievement – why anyone would regard him as a pundit on dna is beyond me).

    Back to the guy who is asking the original questions, while you are experimenting use your cli verbose mode, the verbose application, and in cli: core show help application & core show help function
    …answers to many of the questions you ask are there. Also bear in mind others face nearly all of the challenges you encounter – which has resulted in a rich instruction set. I think you’ll find any familiar programing language constructs are left out as they may bring problems that outweigh usefulness – and there will be another way to do it.

    Finally I can assure you that you can pack an amazing amount of functionality into not to many lines of configuration using a small subset of applications and functions if you choose to…

    John