shell() in a separate thread with callback message at exit?

Sarah Reichelt sarah.reichelt at gmail.com
Sun Mar 11 18:15:40 EDT 2007


On 3/8/07, Joel Guillod <joel.guillod at net2000.ch> wrote:
> How can I implement the following features:
>
> - invoque a shell command in a separate thread, i.e. a non blocking
> shell during execution of the command;
> - receive a callback message with the output and the error result
> when the thread exits?
>
> This would be some function similar to the load command, i.e.:
>
>     shellExecute commandLine [with message callbackMessage]
>
> where
>    The commandLine is a string or an expression that evaluates to a
> string.
>    The callbackMessage is the name of a message to send after the
> shell exited.
>
> The callbackMessage signature would be:
>
>    on <callbackMessage> pCmdOutput,pCmdError,pShellError
>
> where
>    - pCmdOutput is the value returned by the shell function, i.e. the
> result of the sdtout commandLine, including any error messages the
> commandLine generates.
>    - pCmdError is the error the command generate (sdterr under unix).
>    - pShellError is the shell command's exit code.


Hi Joel,

This isn't exactly what you described, but here is how I do it. Start
the shell command running in the background but directing it's output
to a text file. Then have a loop to check for this text file until it
appears or until a time out period has elapsed.

Here is an example script that does a ping. The handler includes the
post-processing of the result, but you can just ignore that.

function checkPing pIP
    put specialFolderPath("Desktop") & "/ping.txt" into tFileName
    if there is a file tFileName then delete file tFileName

    put "ping -c1 -n "  & pIP into tShellCmd
    put " > " & tFileName & " 2>&1 &" after tShellCmd
    get shell(tShellCmd)

    put 0 into timeCheck
    repeat 50 times
        wait 1 tick with messages
        if there is a file tFileName then
            put URL ("file:" & tFileName) into tRes
            if tRes is empty then next repeat  -- file created but no result yet

            put wordOffset("loss", tRes) into tWord
            if tWord = 0 then next repeat -- file created but result
not complete

             put word tWord-2 of tRes into tPercent
            if tPercent = "0%" then return true
            else return false
        end if
    end repeat

    if there is a file tFileName then delete file tFileName
    return false
end checkPing

Cheers,
Sarah



More information about the use-livecode mailing list