# Syntax error Node Exec

**URL:** <https://discourse.nodered.org/t/syntax-error-node-exec/80259>\
**Category:** General\
**Tags:** function-node, exec\
**Created:** [2 August 2023 15:24 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259 "2023-08-02T15:24:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![LinkView\_Maze](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@LinkView\_Maze](https://discourse.nodered.org/u/LinkView_Maze)\
**Post date:** [2 August 2023 15:24 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/1 "2023-08-02T15:24:58Z")

</div>

Hello

I willed create an automatism deleted folder if folder is to old. I have a varible in global with :  
-patch folder : /home/pi/Documents/ANPR/IMAGES/2023  
-nbr\_day : 60

If inject with node inject this command this is ok  
find /home/pi/Documents/ANPR/IMAGES/2023 -mtime +60 -exec rm -r {} ;

I willed execute this fonction with an logic beacause 2023 and 60 are variable but I have one error node function.

 ![node-function](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8e6049adbbe086d01c7912603b9864ae8ae93526.jpeg)  
 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e17cfbe3f1f6db7f7680a51d899bf303cbd9c1b4.jpeg)

```auto
[{"id":"12331c71.416324","type":"inject","z":"a8416f68.4598b","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":1980,"wires":[["bd2836fd.965718"]]},{"id":"bc3aa9e3.1545e8","type":"debug","z":"a8416f68.4598b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":1980,"wires":[]},{"id":"bd2836fd.965718","type":"change","z":"a8416f68.4598b","name":"","rules":[{"t":"set","p":"folder","pt":"msg","to":"filebrowser_default_folder_IMAGES","tot":"global"},{"t":"set","p":"TimeImage","pt":"msg","to":"backimage","tot":"global"},{"t":"set","p":"Year","pt":"msg","to":"Years","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":1980,"wires":[["9b751a56.c5db68"]]},{"id":"9b751a56.c5db68","type":"function","z":"a8416f68.4598b","name":"","func":"msg.payload =\"find \"&msg.folder& \"/\"&msg.Year &\" -mtime +\"&msg.TimeImage &\" -exec rm {} \\;'\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":1980,"wires":[["bc3aa9e3.1545e8"]]}]

```

The result is : `{"_msgid":"c550bbd2.c1c7b8","payload":0,"folder":"/home/pi/Documents/ANPR/IMAGES","TimeImage":15,"Year":"2023"}`

with **payload : 0** different to **find /home/pi/Documents/ANPR/IMAGES/2023 -mtime +60 -exec rm -r {} ;**

Wher is my problem or error ?

Thank you for your help

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 August 2023 15:48 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/2 "2023-08-02T15:48:51Z")

</div>

In the function node you need to use `+` instead of `&` to create the output

```auto
msg.payload ="find " + msg.folder + "/" + msg.Year + " -mtime +" + msg.TimeImage + " -exec rm {} \;'"
return msg;

```

---

<div class="post-metadata">

**Author:** ![LinkView\_Maze](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@LinkView\_Maze](https://discourse.nodered.org/u/LinkView_Maze)\
**Post date:** [2 August 2023 16:09 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/3 "2023-08-02T16:09:30Z")

</div>

Thank you

the new result is : find /home/pi/Documents/ANPR/IMAGES/2023 -mtime +15 -exec rm {} ;  
The "" betenne {} and ; is not here, in function this symbol is in orange

Do you have a solution ?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 August 2023 16:35 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/4 "2023-08-02T16:35:56Z")

</div>

In your original message you have this:

> [@LinkView\_Maze](#):
>
> find /home/pi/Documents/ANPR/IMAGES/2023 -mtime +60 -exec rm -r {} ;

but in your function you did not have the `-r`. You just need to add that in and remove the single `'` at the end.

```auto
msg.payload ="find " + msg.folder + "/" + msg.Year + " -mtime +" + msg.TimeImage + " -exec rm -r {};"

```

---

<div class="post-metadata">

**Author:** ![LinkView\_Maze](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@LinkView\_Maze](https://discourse.nodered.org/u/LinkView_Maze)\
**Post date:** [2 August 2023 16:41 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/5 "2023-08-02T16:41:04Z")

</div>

yes but the real function is :

```auto
msg.payload="find "+msg.folder+"/"+msg.Year+" -mtime +"+msg.TimeImage+" -exec rm -r {} \;"
return msg;

```

and the \ in the end of function doesn't in output .

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 August 2023 17:12 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/6 "2023-08-02T17:12:41Z")

</div>

You might like this approach setting and using environment variables:

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/ba5973ef3892f7293548a355513674652bd5f585.jpeg)

I have not tested it for this scenario, automated deletion of entire directories makes me uneasy.

```auto
[{"id":"afd35866f96a7664","type":"group","z":"812b779e0d82b93f","name":"Passing parameters to a Bash script as environment variables","style":{"label":true},"nodes":["32b53ca9663a2f7a","b9ade47616c81169","fbc7d37039f81c17","3904a3d3cbccc354"],"x":94,"y":351.5,"w":572,"h":129.5},{"id":"32b53ca9663a2f7a","type":"inject","z":"812b779e0d82b93f","g":"afd35866f96a7664","name":"","props":[{"p":"folder","v":"/home/pi/testdata","vt":"str"},{"p":"mtime","v":"60","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":190,"y":400,"wires":[["b9ade47616c81169"]]},{"id":"b9ade47616c81169","type":"template","z":"812b779e0d82b93f","g":"afd35866f96a7664","name":"Bash script ","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"FOLDER={{{folder}}} #ie msg.folder\nYEAR={{{Year}}} # msg.Year\nMTIME={{{TimeImage}}} # msg.TimeImage\nfind $FOLDER/$YEAR -mtime +$MTIME -exec rm -r {} ;","output":"str","x":350,"y":400,"wires":[["3904a3d3cbccc354"]]},{"id":"fbc7d37039f81c17","type":"exec","z":"812b779e0d82b93f","g":"afd35866f96a7664","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"Run the script","x":560,"y":400,"wires":[[],[],[]]},{"id":"3904a3d3cbccc354","type":"debug","z":"812b779e0d82b93f","g":"afd35866f96a7664","name":"Full Script","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":440,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![LinkView\_Maze](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@LinkView\_Maze](https://discourse.nodered.org/u/LinkView_Maze)\
**Post date:** [2 August 2023 17:32 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/7 "2023-08-02T17:32:10Z")

</div>

Thank you

I have modify your respons and it's ok !

```auto
find {{{folder}}}/{{{Year}}} -mtime +{{{TimeImage}}} -exec rm -r {} \;

```

 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e17cfbe3f1f6db7f7680a51d899bf303cbd9c1b4.jpeg)

```auto
[{"id":"12331c71.416324","type":"inject","z":"a8416f68.4598b","name":"Delete Image ANPR to old","props":[{"p":"payload"}],"repeat":"86400","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":1980,"wires":[["bd2836fd.965718"]]},{"id":"bd2836fd.965718","type":"change","z":"a8416f68.4598b","name":"","rules":[{"t":"set","p":"folder","pt":"msg","to":"filebrowser_default_folder_IMAGES","tot":"global"},{"t":"set","p":"TimeImage","pt":"msg","to":"backimage","tot":"global"},{"t":"set","p":"Year","pt":"msg","to":"Years","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":1980,"wires":[["ca2fd52e.3dd1d8"]]},{"id":"d492e759.37c1a8","type":"exec","z":"a8416f68.4598b","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":690,"y":1980,"wires":[[],[],[]]},{"id":"ca2fd52e.3dd1d8","type":"template","z":"a8416f68.4598b","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"find {{{folder}}}/{{{Year}}} -mtime +{{{TimeImage}}} -exec rm -r {} \\;","output":"str","x":560,"y":1980,"wires":[["d492e759.37c1a8"]]}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 August 2023 17:45 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/8 "2023-08-02T17:45:05Z")

</div>

Umm. OK, glad it's working for you!

I developed my method because I wanted to have an entire arbitrary size shell script stored within my Node-red flow.

I confess I am not very familiar with the mustache syntax which I use to set environment variables in the script so I really don't know if it's better or worse combined into a single line like that than your original approach.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 August 2023 18:00 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/9 "2023-08-02T18:00:02Z")

</div>

Show me what the output needs to be.  
Note: I’ll be tied up for the next hour

---

<div class="post-metadata">

**Author:** ![LinkView\_Maze](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@LinkView\_Maze](https://discourse.nodered.org/u/LinkView_Maze)\
**Post date:** [2 August 2023 18:01 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/10 "2023-08-02T18:01:35Z")

</div>

I myself am not very familiar either. But the result is functional so it suits me!

By taking a little of each answers and the ideas of each one we always arrive at our ends.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 August 2023 18:06 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/11 "2023-08-02T18:06:43Z")

</div>

> [@LinkView\_Maze](#):
>
> `find {{{folder}}}/{{{Year}}} -mtime +{{{TimeImage}}} -exec rm -r {} \;`

Make very sure you never run that exec command (or the other versions) when msg.folder and msg.Year are null!

**find / -mtime +60 -exec rm -r {}**

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [2 August 2023 18:15 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/12 "2023-08-02T18:15:47Z")

</div>

There's also an [File Delete](https://flows.nodered.org/node/node-red-contrib-fs-ops) an other file node's  
So handy 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 August 2023 18:15 UTC](https://discourse.nodered.org/t/syntax-error-node-exec/80259/13 "2023-08-16T18:15:52Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
