# Getting return codes from scripts run

**URL:** https://discourse.nodered.org/t/getting-return-codes-from-scripts-run/4991
**Category:** General
**Created:** [18 November 2018 00:02 UTC](https://discourse.nodered.org/t/getting-return-codes-from-scripts-run/4991 "2018-11-18T00:02:39Z")
**Posts on this page:** 6
**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: [18 November 2018 00:02 UTC](https://discourse.nodered.org/t/getting-return-codes-from-scripts-run/4991/1 "2018-11-18T00:02:39Z")

</div>

I have some BASH scripts I want to run on my machines.

If run from the CLI/Terminal, the script works fine.

But as this is run on remote machines I wanted a bit of "feedback" on what is happening.  
I added a line at the end thus:

`return $?`

Which SHOULD return a 0 if all is good, and other numbers if any problems happen.

But NR doesn't seem to like this idea.

This is the node: (Code follows)

```auto
[{"id":"317f1809.c0682","type":"exec","z":"e2bd5a4e.5597e8","command":"bash /home/pi/Mine/NR_Backup.sh","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Backup","x":1180,"y":610,"wires":[[],[],["3ce8bab2.acdb46"]]}]

```

Code:

```auto
#!/bin/bash
# ---------------------------------------
# Simple backup script v1.0
# ---------------------------------------

# Variables
myDate=`date "+%Y-%m-%d.%H.%M.%S"`
backupFolderName="$myDate"
backupSource="/home/pi/.node-red"
backupDest="/home/pi/Backups/NR"
backupFilter="*.j*"
backupExclude="lost\+found"

# Tell the user what we're working with
echo "The myDate variable contains: $myDate"
echo "A backup of $backupSource/$backupFilter will be made and stored in $backupDest/$backupFolderName"

# Begin backup
rsync -avz --progress $backupSource/$backupFilter --exclude=$backupExclude $backupDest/$backupFolderName

# We're done.
echo "Done!"
return $?

```

No, I didn't write the script. I'm not that smart.  
Mine was a lot simpler.

Anyway.....

Running it in NR this happens:

```auto
"{"code":1,"message":"Command failed: bash /home/pi/Mine/NR_Backup.sh\n/home/pi/Mine/NR_Backup.sh: line 24: return: can only `return' from a function or sourced script\n"}"

```

I'm (obviously - and again) missing something.

---

<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: [18 November 2018 00:25 UTC](https://discourse.nodered.org/t/getting-return-codes-from-scripts-run/4991/2 "2018-11-18T00:25:07Z")

</div>

Do you have debug nodes attached to all three outputs of the exec node?

---

<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: [18 November 2018 00:47 UTC](https://discourse.nodered.org/t/getting-return-codes-from-scripts-run/4991/3 "2018-11-18T00:47:48Z")

</div>

Only the bottom one.

But for other things, that seems to work.

The bottom output is the return code..... So...... Isn't that what I want?

 ![Screenshot%20from%202018-11-18%2011-43-53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c39bf4faf2785e21031857b6835f3e0986ad8a53.png)

You will also notice that there are two "TEST" nodes below there.

One does a good command and one is a bash command of "garbage" which will error.

if I run the first one, I get back a 0  
If I run the second one it sends back (not 0) and the other end sees the non-zero return and notifies me the command has/had a problem.

---

<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: [18 November 2018 01:14 UTC](https://discourse.nodered.org/t/getting-return-codes-from-scripts-run/4991/4 "2018-11-18T01:14:05Z")

</div>

After a bit of digging, I think that is more an O/S problem than NR.

Sorry.

Shall continue digging.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [18 November 2018 08:04 UTC](https://discourse.nodered.org/t/getting-return-codes-from-scripts-run/4991/5 "2018-11-18T08:04:26Z")

</div>

You don't use `return` to exit a shell script - just as the error message tells you.

You would use `exit $?`

However, `$?` is the return code of the last command run. In you script, you are using `$?` after a call to `echo` - so will contain the return code of that call. So it won't tell you anything about whether the rsync worked or not.

You'd need to capture the return code of the call to rsync:

```auto
# Begin backup
rsync -avz --progress $backupSource/$backupFilter --exclude=$backupExclude $backupDest/$backupFolderName
RC=$?
# We're done.
echo "Done!"
exit $RC

```

---

<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: [19 November 2018 03:31 UTC](https://discourse.nodered.org/t/getting-return-codes-from-scripts-run/4991/6 "2018-11-19T03:31:34Z")

</div>

Thanks very much Nick.

I guess I didn't specify that in the bigger picture.
