[asterisk-users] Correct syntax for IF()?

Adam Moffett adam at plexicomm.net
Mon Nov 26 20:23:59 CST 2007


A simpler example reveals the problem:

        exten => 188,1,Noop(${STAT(e,/bin/ls)})
        exten => 188,2,Noop(${STAT(e,/not/there)})

Try that and you'll find that STAT(e,/whatever) returns 1 if the file is 
found and NOTHING if the file is not found.

This method should work:

${IF($["${STAT(e,/tmp/${CALLTIME}.wav)}" = "1"]?${CALLTIME}.wav:"")}


So if the file exists you'll have
"1" = "1" (true)
if not you'll have
"" = "1" (false)

Keep in mind that quotations marks don't mean anything special to 
asterisk.  It's just another character, and here they're just used to 
enclose the empty string that is returned by STAT.   We could probably 
use any other character for this purpose, but quotes probably seem more 
natural to us English speakers.

<crybaby mode>
This bizarre syntax is the thing I hate most about asterisk.  For people 
like you and me with some scripting or programming experience it would 
seem natural to use IF the way you wanted to, but asterisk is just weird 
in this way.
</crybaby mode>

> On Mon, 26 Nov 2007 23:40:37 +0100, Turbo Fredriksson
> <turbo at bayour.com> wrote:
>   
>> What you do is you always write the beginning _and_ the end at once. Never try to do them
>> 'later'...
>>     
>
> Thanks guys. I think I found where it goes wrong:
>
> ======
>
> 1. /tmp/test.wav exists -> the $[] is true:
>
> exten => h,n,Set(CALLTIME=test)
> exten =>
> h,n,Set(WAV_FILE=${IF($[${STAT(e,/tmp/${CALLTIME}.wav)}]?${CALLTIME}.wav:"")})
> exten => h,n,Verbose(WAV_FILE is ${WAV_FILE})
>
> ======
>
> 2. /tmp/dummy.wav doesn't exist -> the $[] is false:
>
> exten => h,n,Set(CALLTIME=dummy)
> exten =>
> h,n,Set(WAV_FILE=${IF($[${STAT(e,/tmp/${CALLTIME}.wav)}]?${CALLTIME}.wav:"")})
> exten => h,n,Verbose(WAV_FILE is ${WAV_FILE})
>
> => WARNING[5296]: func_logic.c:107 acf_if: Syntax
> IF(<expr>?[<true>][:<false>])
>
> ======
>
> For the "false" part, I tried the following, none works:
> - nothing
> - :
> - :""
>
> Is it a known bug, and does Asterisk 1.4.14 solve this?
>
> Thank you.
>
>
> _______________________________________________
> --Bandwidth and Colocation Provided by http://www.api-digital.com--
>
> asterisk-users mailing list
> To UNSUBSCRIBE or update options visit:
>    http://lists.digium.com/mailman/listinfo/asterisk-users
>
>
>   




More information about the asterisk-users mailing list