Returning an exit code from a bash function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Returning an exit code from a bash function
# 1  
Old 01-20-2017
Returning an exit code from a bash function

How to return a exit code from a function and use it in conditional?
I tried the following but it does not seem to work.

tests.sh:
Code:
if test ./load.sh ; then
    echo "0"
else
    echo "1"
fi

load.sh:
Code:
return 1;

from command line:
Code:
$ ./tests.sh
0

I was expecting it to output "1" Smilie
Thank you.
# 2  
Old 01-20-2017
I'm confused. The return 1 statement can only be used in a function or a sourced shell script. This doesn't apply here?

Are you sure you are using bash for this? If you do not specify:
Code:
#!/bin/bash

as the very first line of test.sh then you are using
Code:
/bin/sh

which is a POSIX shell, not necessarily bash.
Code:
 echo $SHELL

will tell you ( inside test.sh) what shell you are using.

So help us to help you with some more information.

PLUS
a return code of zero means success in the shell, any other number is a fail.
# 3  
Old 01-20-2017
PS: this is what I get for test.sh and my system

Code:
Owner@Owner-PC ~
$ chmod +x test.sh

Owner@Owner-PC ~
$ ./test.sh
./test.sh: line 2: return: can only `return' from a function or sourced script

Owner@Owner-PC ~
$ bash --version
GNU bash, version 4.4.5(1)-release (x86_64-unknown-cygwin)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

I do not want to boot to linux && have to shutdown my windows stuff right now - so cygwin.
# 4  
Old 01-20-2017
Quote:
Originally Posted by jim mcnamara
I'm confused. The return 1 statement can only be used in a function or a sourced shell script. This doesn't apply here?
Sorry I left out the bash header. I am using Bash.

I have several load.sh scripts, and want to call them all from one test.sh script.
Each load.sh script calls a C++ function that returns an error code (0 means EXIT_SUCCESS).
Is there a way to pass the error code from C++ function, to load.sh, to test.sh?
And then test.sh use the returned error code in a conditional statement?

Thanks.

tests.sh:
Code:
#!/bin/bash

if test ./load.sh ; then
    echo "0"
else
    echo "1"
fi

load.sh:
Code:
#!/bin/bash

return main

Code:
int main()
{
    return 1; //1 means EXIT_FAILURE
}

from command line:
Code:
$ g++ main.cpp -o main
$ ./tests.sh
0

I was expecting it to output "1".
# 5  
Old 01-20-2017
The bash shell script:
Code:
#!/bin/bash

return main

does not make any attempt to run the utility named main. And, as jim mcnamara already said, you don't use return to exit from a bash shell script. According to the standards, using return when you are not inside a function or inside a script invoked by the . command produces unspecified results. Running that script with bash prints two diagnostic messages:
Code:
load.sh: line 2: return: main: numeric argument required
load.sh: line 2: return: can only `return' from a function or sourced script

and exits with exit code 1. Running that same script with ksh produces no diagnostics and exits with exit code 0.

If you just want a shell script that invokes a utility named main and exits with an exit code matching that returned by main, you can use any of the following:
Code:
#!/bin/bash
main
rc=$?
if [ $rc -eq 0 ]
then	exit 0
else	exit $rc
fi

or:
Code:
#!/bin/bash
main
rc=$?
if [ $rc -eq 0 ]
then	exit 0
fi
exit $rc

or:
Code:
#!/bin/bash
main
rc=$?
exit $rc

or:
Code:
#!/bin/bash
main
exit $?

or, more simply, just:
Code:
#!/bin/bash
main

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 01-20-2017
Thanks Don Cragun.

I tried the scripts you suggested in my load.sh file, but get the same result.

load.sh:
Code:
#!/bin/bash
main
rc=$?
exit $rc

From command line:
Code:
$ ./tests.sh
0

Bash is not giving may any messages when I run tests.sh. Yet jim mcnamara and you are seeing messages.
Is that something I can turn on?

However, I do get a message when I run load.sh directly:
Code:
$ ./load.sh
./load.sh: line 2: main: command not found
127
$ ls
load.sh  load.sh~  main  main.cpp  main.cpp~  tests.sh  tests.sh~

---------- Post updated at 03:20 AM ---------- Previous update was at 03:15 AM ----------

Update:

Adding "./" to load.sh fixed load.sh:
Code:
#!/bin/bash
./main
rc=$?
echo $rc
exit $rc

From terminal:
Code:
$ ./load.sh
1


But tests.sh still prints 0:
Code:
$  ./tests.sh
0

I was expecting it to output "1".
# 7  
Old 01-20-2017
Try:
Code:
if ./load.sh ; then
    echo "0"
else
    echo "1"
fi

--
See test: operands
Code:
string
True if the string string is not the null string; otherwise, false.

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

[BASH] Script to manage background scripts (running, finished, exit code)

Heyas, Since this question (similar) occur every now and then, and given the fact i was thinking about it just recently (1-2 weeks) anyway, i started to write something :p The last point for motivation was... (17 Replies)
Discussion started by: sea
17 Replies

3. Programming

Malloc function returning NULL

Hi All, I am using malloc function for allocating dynamic memory. When I am using below code on Linux server its working fine, but When I am trying the same code on HP UNIX server its returning NULL. below is a fragment of code in which it is giving problem. tmp = (format_tree... (4 Replies)
Discussion started by: Taher Saifuddin
4 Replies

4. UNIX for Advanced & Expert Users

Returning exit code from a while read $inputline

Hi searched hi and wide for this with no luck. Maintaining a bash script that #!/usr/bin/bash #does some stuff like setting env etc. f_do_dbwork ... .. #Now I want to exit with the value of $err however $err is re-initialised to 0 on exiting the function Always 0 .... ... (3 Replies)
Discussion started by: StevePr
3 Replies

5. Programming

Function Returning Pointer

Hi guys. how a functions such fdopen, ... can return pointer? are these functions use static memory(variables)? (6 Replies)
Discussion started by: majid.merkava
6 Replies

6. Shell Programming and Scripting

Returning the name of function used

Hi All In my script, I can call on several functions. I have a logging function that is called by any of these functions. What I would like is some way of identifying which function I am using and pass this to the log function as some parameter. Is there some built in command or way of... (3 Replies)
Discussion started by: kingpin2502
3 Replies

7. Shell Programming and Scripting

[Bash]Function returning a boolean

Hello all, I would like to know if it is possible to return a the result of a boolean expression from a function like this function() { # some code return || } and what will be the return value ? Thank you for help. (6 Replies)
Discussion started by: dolphin06
6 Replies

8. Programming

returning multiple values from a function in C

hi how can I return multiple values from a C function. I tried the following: #include <stdio.h> void foo(int id, char *first_name, char *last_name) { /* this is just an example to illustrate my problem... real code makes use of the "id" parameter. */ first_name = (char... (8 Replies)
Discussion started by: Andrewkl
8 Replies

9. Shell Programming and Scripting

returning from a function

Hi all, I am very new to BASH shell programming. I need to return an integer from a function to the caller function. I did this: but it keeps giving me wrong return: Can someone help me out here, please? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

10. Programming

string returning function

I have two string returning function in ESQL/C char *segment_name(lbuffer) char *lbuffer; {..... and char *get_bpdvalue(f_name) char *f_name; {...... both declared above main() char *get_bpdvalue(); char *segment_name(); my problem is segment_name works on sprintf and strcpy... (5 Replies)
Discussion started by: jisc
5 Replies
Login or Register to Ask a Question