# Help with shell command to get a result in a node

**URL:** <https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548>\
**Category:** General\
**Created:** [15 September 2019 08:03 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548 "2019-09-15T08:03:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 September 2019 08:03 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/1 "2019-09-15T08:03:05Z")

</div>

I am wanting to fix a `message` I am getting.

This is the message:  
`{"_msgid":"3c5b556c.b18d0a","topic":"","payload":" Used\n 688\n","rc":{"code":0}}`

What I want to get is the amount of usage on a USB stick and map it to a `gauge`.

I found the command:  
`df --output=used /dev/sda1`  
(If that can be made better, all the better.)

So I am getting the message above and want to _turn it into_ a simple (integer) 688.

I've tried things but am not understanding how to do it.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 September 2019 08:17 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/2 "2019-09-15T08:17:56Z")

</div>

You could probably do it with JSONata (but I have no clue how).

I would first look up the `df` command help see if any command line parameter might simplify the output. If not, then I'd use a regular expression. Something like `(\d+)`.

[Here is a working example](https://regex101.com/r/bX1snR/1)

Ps, that site also generates the is code for you to use in a function block (left have menu, generate code)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 September 2019 08:21 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/3 "2019-09-15T08:21:07Z")

</div>

Thanks Steve.

I worked out if I `string` and `delLeftmost` to a `\n` I get 688.  
But then I put it through a `function` node and convert it to an `int` value.

```auto
var x = parseInt(msg.payload);
msg.payload = x;
return msg;

```

Then send it to the `gauge` node. Looks good.

But appreciated.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [15 September 2019 08:29 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/4 "2019-09-15T08:29:01Z")

</div>

You don't even need to create the x variable  
`msg.payload = parseInt(msg.payload)`  
will do the trick nicely

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 September 2019 08:31 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/5 "2019-09-15T08:31:04Z")

</div>

Yeah,

I think that is a layover from other problems parsing variables where sometimes it can't deal with something like `msg.payload`.

Or have I got that bit wrong too?  
(Sorry. Brain drain happening.)

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [15 September 2019 08:35 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/6 "2019-09-15T08:35:31Z")

</div>

> [@Steve-Mcl](#):
>
> You could probably do it with JSONata (but I have no clue how).

Or in JSONata in a change node after the command output, set as Payload to JSONata expression.  
`$number($match(payload, /(\d+)/)[0]['groups'][0])`  
(Typing from phone, pretty sure it’s typo free but untested)

What this does is execute the regex on the payload string to get the number out but still as a string, then parse it to a number and return it.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [15 September 2019 10:33 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/7 "2019-09-15T10:33:56Z")

</div>

Yet another way - grep the result from the original command via a pipe

```
df --output=used / | grep -v Used

```

then just feed into the gauge (as it will cope with simple text.)

```auto
[{"id":"425a6c38.bcad84","type":"inject","z":"df0a9f4b.bc75b","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":200,"wires":[["4f93063c.c91df8"]]},{"id":"4f93063c.c91df8","type":"exec","z":"df0a9f4b.bc75b","command":"df --output=used / | grep -v Used","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":370,"y":200,"wires":[["3af9a86f.268f78"],[],[]]},{"id":"3af9a86f.268f78","type":"ui_gauge","z":"df0a9f4b.bc75b","name":"","group":"1fc20d40.3dd9d3","order":0,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":"10000000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":630,"y":200,"wires":[]},{"id":"1fc20d40.3dd9d3","type":"ui_group","z":"","name":"Default","tab":"92544f31.05d25","disp":true,"width":"6","collapse":false},{"id":"92544f31.05d25","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 September 2019 11:41 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/8 "2019-09-15T11:41:38Z")

</div>

Sooo, many ways! Aren't computers fun 😺

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 September 2019 20:36 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/9 "2019-09-15T20:36:47Z")

</div>

(How embarrassing.)

I already had most of that _flow_ already. I just hadn't added the _GUI_.

One way I did it was:  
`df -h /dev/sda1 | grep / | tr -s ' ' | cut -d " " -f4`  
But that shows me the _remaining_ spavce - I think. I didn't know the `--output=used` bit then.

And the way I did it last night was:  
`df --output=used /dev/sda1`  
(but then needed to do work on the output with other `nodes`.

Sorry guys.

It is called C.R.A.F.T. disease.

I'll let you work that out.

> then just feed into the gauge (as it will cope with simple text.)

@dceejay Yeah, oops. Silly me.

Oh, not to sound petty.

Ok, so that part works.

It is/seems silly having that and not having a _threshold_ above which it alerts me.  
So I send the output to a `switch` and switch if the value \> (what ever)

So I am guessing I will need a `function` node in there with `msg.payload = parseInt(msg.payload)` in it.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [16 September 2019 04:25 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/10 "2019-09-16T04:25:11Z")

</div>

I remember a very, very old ad from the late 70's...it was a cartoon and it was an ad for,,,yes,,,IBM

_Two men standing looking at a construction site_  
_- if it wasn't for that excavator, 100 more men with shovels would have a decent job_  
_- yeah, and if it wasn't for those shovels, it could be 1000 men with teaspoons_

---

<div class="post-metadata">

**Author:** ![clickworkorange](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/clickworkorange/32/12109_2.png) [@clickworkorange](https://discourse.nodered.org/u/clickworkorange)\
**Post date:** [16 September 2019 19:02 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/11 "2019-09-16T19:02:06Z")

</div>

Just a tip; there's [a set of very handy system nodes](https://flows.nodered.org/node/node-red-contrib-os), which includes a disk-space node, as well as CPU usage, free RAM, and system uptime. They use the [Node.js OS library](https://nodejs.org/api/os.html) and are compatible with both Linux and Windows. I've been using these on a Pi for the last couple of years with zero issues.  
 ![Screenshot_2019-09-16_19-59-36](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/5fa6c7728b1db104a1b42a1d42b3f5c72b7eb020.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 September 2019 20:48 UTC](https://discourse.nodered.org/t/help-with-shell-command-to-get-a-result-in-a-node/15548/12 "2019-09-16T20:48:35Z")

</div>

Looks nice.

Thanks.

Shall install and use.
