bash command  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

bash command

Post 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
}

Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
willemd
Posts: 741
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: bash command  [Solved]

Post 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\"' ..
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: bash command

Post by pvklink »

Ok!, i will try...
And what about the part at the end?, the " \;" ?
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
willemd
Posts: 741
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: bash command

Post 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.
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: bash command

Post 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...
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
willemd
Posts: 741
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: bash command

Post 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.
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: bash command

Post 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!
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest