Bash commands and the exec node

Flushed with some mild success, I thought I'd try something else before I give it away for the day.

I've been able to run a simple Python script that prints a number series and now I'm wondering if I can run some bash commands. If I enter echo "test" on the Command line of the exec node then test is displayed as an output from the debug node. Say I want to display my pi directory with the commands:

cd /home/pi
ls

How do I enter those two commands into the exec node? Searching the forum hasn't revealed an answer.

Well, I guess you could do that.

There is/are better ways of doing what you want though.

Without wanting to pry too much, what exactly do you want to do?

edit

For the sake of answering your question:

You would not do the two commands, but more:
ls /home/pi
which gets around needing two lines.

Just connect an inject node to the exec node, then connect a debug node to the top output of the exec node.

Open the exec node and untick the include msg.payload button.

Enter ls /home/pi in the line where you put the command.

Save that and deploy.

Press the inject node and you should see a list of files/directories from /home/pi

Silly me, I should have thought of "ls /home/pi", it's been a long hot day. Still, is it possible to enter multiple commands?

Regarding my motive, I have nothing in mind, just experimenting. What is the better way to run a Python script. I notice that there is a python shell in the contrib library, is that what you have in mind?

Nothing wrong with just trying things. I do that a lot.

But saying that, it isn't possible to do multiple commands.

(Why would you?)

If the scenario arises where you need to do multiple commands:
make a script file. (.sh) and run it.

So..... eg:

/home/pi/something.sh

with that file being executable and correctly structured.

A shell script, of course.

One last question. Is there a node that displays text in a box? For example, instead of feeding exec into a debug node could the text be properly formatted? There doesn't appear to be anything suitable in the dashboard list of nodes

If you click on the debug message you can "expand" the message and it is (will be) displayed properly.

Where you are now, you are more looking at the EDIT part rather than the GUI side of things.

Don't get too bogged down on what you see here as it is not really going to be useful when you are looking at a GUI.

I see what you mean with the debug output.

I'm more interested in displaying text in a GUI box, even though I don't a real reason to do so at the moment.

Then you use a TEXT node and send the message into it. I think.

Sorry, I am a bit out of touch with that sort of stuff only that I haven't really done it AFAIK.

I'm just now diving into Python stuff so I am maybe (probably?) not the best person to really help you.

Don't be afraid to try things. Try the text node. But to do that, you need to make a dashboard and set all that up.

Not rocket science, but it takes a bit of doing.
(Sorry, the words aren't coming out as I want just now.)

I had already tried the text node but it doesn't seem to do what I have in mind.

Anyway, thank you for answering my questions.

Yeah, sorry. I have never needed to do what you want so ......

:man_shrugging:

  1. You can concatenate command in linux using && so cd /home/pi && ls should work

  2. You can use a ui_template node - set the size as you require - and change the default template to be

<pre><div ng-bind-html="msg.payload"></div></pre>
The template expects html - but adding pre means the carriage returns in the text are then honoured.

example flow

[{"id":"97d389df.a74008","type":"inject","z":"78444ab3.45a654","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":200,"y":540,"wires":[["8d5af63e.ae2ef8"]]},{"id":"8d5af63e.ae2ef8","type":"exec","z":"78444ab3.45a654","command":"cd ~ && ls -l","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":390,"y":540,"wires":[["7f4d00cc.09979"],[],[]]},{"id":"7f4d00cc.09979","type":"ui_template","z":"78444ab3.45a654","group":"a223b12f.6e05a","name":"","order":0,"width":"6","height":"8","format":"<pre><div ng-bind-html=\"msg.payload\"></div></pre>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":580,"y":540,"wires":[[]]},{"id":"a223b12f.6e05a","type":"ui_group","name":"Default","tab":"45c56a9a.0646c4","order":1,"disp":true,"width":"6","collapse":false},{"id":"45c56a9a.0646c4","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

Just to clarify that && only runs the second command if the first completes with a zero return code. e.g. it succeeds.

Details are here:

https://www.gnu.org/software/bash/manual/html_node/Lists.html

The correct way to run multiple commands regardless of success or failure is to separate them with a ;. Windows users should note that this works with the cmd.exe shell as well. && is more commonly used because you generally want subsequent commands to fail if an earlier one fails.

2 Likes

Thank you for the template example, I'll give it a test.

TotallyInformation, I've come across the use of a semicolon as a separator in the past, I'll also give that a test as well.

Thank you for the example, it does exactly what I had in mind.

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