Help with how to better construct this message using GLOBAL context

Ok: me being anal, or not. :person_shrugging:

I have this in an exec node:
tail -n11 /media/pi/9020-9C27/events/commands_received.txt | cat
Which is fair enough.

But I also have this in global context:
event_paths as /media/pi/9020-9C27/events/ and
log_paths as /media/pi/9020-9C27/logs/ (Ok not needed here, but....)

So I am wanting to translate the event_paths into the command.

(As best I can explain it showing the structure)

tail -n11 *globalContext_even_paths*commands_received.txt | cat

The nodes:

[{"id":"74647831.e6bf18","type":"inject","z":"a3bd09a.b852978","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":210,"wires":[["88ec09d0.6e1c28"]]},{"id":"88ec09d0.6e1c28","type":"exec","z":"a3bd09a.b852978","command":"ls -1 --file-type /media/pi/9020-9C27/logs/reboot/ | grep -v '/' | sed s/@$// | cat","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Cat","x":470,"y":210,"wires":[["17917965.d43657","87a8c9d9.4f7f58"],[],[]]}]

Any ideas please?

Why not construct your command line in a function or template node and pass the whole lot to exec as msg.payload, leaving Command in the exec node blank?

Incidentally, I'm pretty sure that your trailing | cat does nothing useful, though it does no harm either.

1 Like

I guess that would be the simple way.

Thanks.

I seem to have this ability to make simple things difficult for myself more times than I should.

I'll give it a go.

Or

[{"id":"88ec09d0.6e1c28","type":"exec","z":"53a4dcf62732d6d9","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"Cat","x":1000,"y":1260,"wires":[["e3a8afb54bab0451"],["e3a8afb54bab0451"],["e3a8afb54bab0451"]],"info":"\"ls -1 --file-type \" & $globalContext(reboot) & \" | grep -v '/' | sed s/@$// | cat\""},{"id":"f0ea16b4c993cc86","type":"inject","z":"53a4dcf62732d6d9","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"ls -1 --file-type \" & $globalContext(\"reboot\") & \" | grep -v '/' | sed s/@$// | cat\"\t","payloadType":"jsonata","x":830,"y":1260,"wires":[["88ec09d0.6e1c28"]],"info":"\"ls -1 --file-type \" & $globalContext(reboot) & \" | grep -v '/' | sed s/@$// | cat\"\r\n\"tail -n11 \" & $globalContext(\"event_paths\") & \"commands_received.txt | cat\""}]

(Mind blown.)

But it is (at this point in time) MAGIC.

The command
"ls -1 --file-type " & $globalContext("reboot") & " | grep -v '/' | sed s/@$// | cat"
is way beyond my skill set. (Not your problem)

But I can't draw a comparison to the original command:
tail -n11 *globalContext_even_paths*commands_received.txt | cat

ls vs tail They are two very different commands.

I'll run off and do some digging and see what I learn.

But thanks.

IGNORE THIS REPLY
Sorry, I goofed.

Left in because I'm dumb.

Ok, sorry I didn't declare this at the start.

The command is being used in a watch node.
Monitoring file activity in the given directory.

Alas the watch node (both file and directory) doesn't have an input.

In your original post you talked about
tail -n11 /media/pi/9020-9C27/events/commands_received.txt | cat
which as you know prints the last 11 lines in the file.

But in your sample flow the exec node is set to run
ls -1 --file-type /media/pi/9020-9C27/logs/reboot/ | grep -v '/' | sed s/@$// | cat
which lists the files in the reboot directory, ignoring subdirectories and stripping any @ symbols off the end of the filename.

So they are indeed very different commands. :grinning_face_with_smiling_eyes:
Which one are you trying to parameterise with the global context variable?

Maybe you should share a bit more of the flow (not all of it please!)

1 Like

You are right.

I got confused. So many things happening.

Ignoring my incorrect post (number 5)....

I am wanting to display the last few lines of the reboot log file.

The rub (for me) is that I am entering the entire path - which is fixed.

Small part of the code:

[{"id":"74647831.e6bf18","type":"inject","z":"a3bd09a.b852978","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":210,"wires":[["88ec09d0.6e1c28"]]},{"id":"88ec09d0.6e1c28","type":"exec","z":"a3bd09a.b852978","command":"ls -1 --file-type /media/pi/9020-9C27/logs/reboot/ | grep -v '/' | sed s/@$// | cat","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Cat","x":470,"y":210,"wires":[["17917965.d43657","87a8c9d9.4f7f58"],[],[]]},{"id":"17917965.d43657","type":"split","z":"a3bd09a.b852978","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":670,"y":200,"wires":[["f8e85ffb.27a12","a2f4ed08.9b76d"]]},{"id":"f8e85ffb.27a12","type":"switch","z":"a3bd09a.b852978","name":"","property":"payload","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":670,"y":250,"wires":[["8d28b379.4fc7b","4df38dde.8dd85c"]]},{"id":"4df38dde.8dd85c","type":"debug","z":"a3bd09a.b852978","name":"3","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":840,"y":250,"wires":[]}]

The switch node may not be needed. It is included .... yeah. I'm not sure.
But as it, it works.
I think it is to handle the case if the file is empty. But I 'm not sure it is the right way of doing it.

So....

The problem is that if ever I change the USB stick to another one.

I just edit the global context and don't need to also edit this node. (And others like it)

Probably as suggested in reply 2 I do it that way.

Ok, expansion on things.

Here is another (sorry) bit of the code.

[{"id":"74647831.e6bf18","type":"inject","z":"a3bd09a.b852978","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":210,"wires":[["88ec09d0.6e1c28"]]},{"id":"88ec09d0.6e1c28","type":"exec","z":"a3bd09a.b852978","command":"ls -1 --file-type /media/pi/9020-9C27/logs/reboot/ | grep -v '/' | sed s/@$// | cat","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Cat","x":470,"y":210,"wires":[["17917965.d43657","87a8c9d9.4f7f58"],[],[]]},{"id":"17917965.d43657","type":"split","z":"a3bd09a.b852978","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":670,"y":200,"wires":[["f8e85ffb.27a12","a2f4ed08.9b76d"]]},{"id":"a2f4ed08.9b76d","type":"debug","z":"a3bd09a.b852978","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":840,"y":200,"wires":[]},{"id":"f416f19bd14acba1","type":"inject","z":"a3bd09a.b852978","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1010,"y":140,"wires":[["3b8e8db1663c4cfe"]]},{"id":"3b8e8db1663c4cfe","type":"change","z":"a3bd09a.b852978","name":"command","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"ls -1 --file-type \" & $globalContext(\"reboot\") & \" | grep -v '/' | sed s/@$// | cat\"\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1180,"y":140,"wires":[["f84266302b2910d0"]]},{"id":"f84266302b2910d0","type":"exec","z":"a3bd09a.b852978","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":1400,"y":140,"wires":[["17917965.d43657"],[],[]]}]

Desired.

What the new code gives me.

I'll see if I can work out where the problem is.
I'm guessing it is getting a list of all files rather than the one I want.

AH!

Removing the wire between the change and the exec node (on the right) and replacing it with a debug I get this strange output:

But the original command in theoriginal exec node is this command:
ls -1 --file-type /media/pi/9020-9C27/logs/reboot/ | grep -v '/' | sed s/@$// | cat

Slight difference.

Confused.

Ah2!

Is this this what should be in the change node?
"ls -1 --file-type " & $globalContext("log_path") & "reboot | grep -v '/' | sed s/@$// | cat"

That seems to be giving me the expected result.

RESOLVED!

Thanks folks.

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