Page 1 of 1

bash command

Posted: Tuesday 07 March 2023 14:31
by pvklink
Hi,

I have the follwing script working. Is checks if there are file on a folder and if so , move them to another folder. Accept for file gelukt.txt and file that starts with XXX.
This scripts works and uses a bash script to move the files.
I tried to script the move command from the working bash script in this script (see REMARK 1-3) but that didnt work.
Can you help ?

Code: Select all

local timer_overdag = 'every 15 minutes between 06:20 and 23:50 on mon,tue,wed,thu,fri,sat,sun'

return {
    on =    { 
            timer = {timer_overdag},
            devices = {'filecopy'},
            },
            
    execute = function(dz,item,info)

    local sourceDir = '/mnt/mymotion'
    local targetDir = '/mnt/nasdata'
    
    function exec_os(cmd)
        local f = assert(io.popen(cmd, 'r'))
        s = assert(f:read('*a'))
        f:close()
        return tonumber(s)
    end
 
    if (item.isDevice and item.name == 'filecopy' and item.state == 'On') or (item.isTimer and item.trigger == timer_overdag) then
    
        cmd='ls /mnt/myimages -I gelukt.txt | wc -l'
        if exec_os(cmd) > 0 then
            dz.devices('motion files aanwezig').switchOn().checkFirst()

            -- REMARK 1. NOW IN BASHSCRIPT -->     find $sourceDir -type f -not -name "xxx*" -and -not -name "gelukt.txt" -exec mv {} $targetDir \;
            -- REMARK 2. TRY TO EXECUTE ABOVE CMD FROM THIS SCRIPT  --> cmd="find " .. sourceDir .. " -type f -not -name " .. '"xxx*"' .. " -and -not -name " .. '"gelukt.txt"' .. " -exec mv {} " .. targetDir .. " \;" 
            -- REMARK 3. DOES NOT WORK, SO USE THE BASH SCRIPT INSTEAD, see below cmd. In this bashscript line 1. is used
            cmd="/opt/domoticz/userdata/filecopy.sh filecopy"

            exec_os(cmd)
        else
            dz.devices('motion files aanwezig').switchOff().checkFirst()
        end
        dz.devices('filecopy').switchOff().afterSec(5)
    end

end
}


Re: bash command  [Solved]

Posted: Tuesday 07 March 2023 15:10
by willemd
You probably have to use a backslash in front of the double quotes around xxx and gelukt to let dzVents know this double quote should be taken as-is and not interpreted as string start/end.

So use .. '\"xxx*\"' ..
and .. '\"gelukt.txt\"' ..

Re: bash command

Posted: Tuesday 07 March 2023 16:16
by pvklink
Ok!, i will try...
And what about the part at the end?, the " \;" ?

Re: bash command

Posted: Tuesday 07 March 2023 16:23
by willemd
pvklink wrote: Tuesday 07 March 2023 16:16 Ok!, i will try...
And what about the part at the end?, the " \;" ?
I am not sure, maybe you need a double backslash.
If you log the cmd into the log file you probably can see whether the result is correct.

Re: bash command

Posted: Tuesday 07 March 2023 16:49
by pvklink
ok, little bit further!
1. this is working line in bash file: find $sourceDir -type f -not -name "xxx*" -and -not -name "gelukt.txt" -exec mv {} $targetDir \;
2. This line in dzvents gives the same line when i print it...
cmd="find " .. sourceDir .. " -type f -not -name " .. '\"xxx*\"' .. " -and -not -name " .. '\"gelukt.txt\"' .. " -exec mv {} " .. targetDir .. " \\;"but it does not

work, and the first line in the bash does...

Re: bash command

Posted: Tuesday 07 March 2023 16:55
by willemd
I don't see the error right now. I suggest you try to find the error by starting with a simpler cmd and build up gradually to the final desired cmd.

Re: bash command

Posted: Tuesday 07 March 2023 16:55
by pvklink
ahh, my mistake, it works! thanks willemd !
the start-end/ characters did the trick and yes an extra / with the last / also..

thanks again!