Shell commands are blocking -- work around?

Sarah Reichelt sarah.reichelt at gmail.com
Sun Nov 12 18:52:46 EST 2006


On 11/11/06, Sivakatirswami <katir at hindu.org> wrote:
> Some on our team are *nix nerds and if I need a powerdrill to add to my
> tool box they are, being long time masters of the unix  patchwork quilt
> method of micro programs and  pipes...  more than happy to give me
> a little bash script. OK so, my cmd line skills are slowly improving and
>   I can install these widgets in
>
> /usr/local/bin/some_cool_tool.sh  # on my OSX box, G4 Powerbook
>
> e.g.
>
> SEARCH_PATTERN=$1
> locate index.shtml | while read INDEX_FILE; do
>    fgrep -H -i "$SEARCH_PATTERN" $INDEX_FILE
> done | more
> exit 0
>
> (wow, that is soooo concise!)
>
> and  then in Revolution set up a button:
>
> on mouseUp
>    put empty into fld "results"
>    set the shellCommand to "/bin/sh"
>    put "/usr/local/bin/web_content_search.sh" & quote & fld "findString"
> & quote
> into tShellCmd
>    put shell (tShellcmd) into fld "results"
> end mouseUp
>
> It all works, is very "sweet" And it is a  *lot* faster than using
> transcript
>   for the same job. *but* Only problem is: it is blocking... and this is
> documented behavior:
>

Here is my way around this problem. The example below is for a "ping"
command, but I'm sure you can adapt it to your stuff.

As you can see, I direct the output of the shell command to a
temporary text file. The second part of the shell command containing
the "2>&1 &" is the relevant section. I don't understand it but it
works :-) The rest of the script just loops around checking to see if
there is anything in this file and then parsing the result. Since it
uses wait with messages, it is non-blocking.

HTH,
Sarah

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
        add 1 to timeCheck
        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

            -- if there is a file tFileName then delete file tFileName
            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



More information about the Use-livecode mailing list