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
# 15  
Old 01-20-2017
Quote:
Originally Posted by RudiC
What if you run ../tut0/load.sh without test? And, how about setting bash's xtrace option?
I didn't know about xtrace. Nice. Thanks for the tip.
Running ../tut0/load.sh directly works fine.
But test.sh is not calling ../tut0/load.sh.

$ cat tests.sh
Code:
#!/bin/bash
echo "in tests.sh --------"
pwd
ls
ls ../tut0

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

$ cat ../tut0/load.sh
Code:
#!/bin/bash
echo "in load.sh --------"
pwd
ls
ls ../suite

../suite/main
rc=$?

From the terminal:
Code:
$  bash -x ./tests.sh
+ echo 'in tests.sh --------'
in tests.sh --------
+ pwd
/home/wolfv/Documents/developer/c++/demo/test_script/suite
+ ls
load.sh  load.sh~  main  main.cpp  main.cpp~  tests.sh	tests.sh~
+ ls ../tut0
load.sh  load.sh~
+ test ../tut0/load.sh
+ echo 0
0

I was expecting output to also contain the line "in load.sh --------".
Why is test.sh not calling ../tut0/load.sh?Smilie

Running load.sh directly, pwd prints the directory it was called from, rather than the file's directory:
Code:
$  bash -x ../tut0/load.sh
+ echo 'in load.sh --------'
in load.sh --------
+ pwd
/home/wolfv/Documents/developer/c++/demo/test_script/suite
+ ls
main  main.cpp	main.cpp~  tests.sh  tests.sh~
+ ls ../suite
main  main.cpp	main.cpp~  tests.sh  tests.sh~
+ ../suite/main
in main()-------
+ rc=1
+ echo rc:
rc:
+ echo 1
1
+ exit 1

# 16  
Old 01-20-2017
Remove the test, let if interpret the exit code of load.sh immediately.
# 17  
Old 01-20-2017
Quote:
Originally Posted by RudiC
Remove the test, let if interpret the exit code of load.sh immediately.
That fixed it! Smilie
Thank you RudiC and everyone else that helped along the way.
I could not have done it without you.

The following example works as intended.

$ cat tests.sh:
Code:
#!/bin/bash
echo "in tests.sh --------"
if ../tut0/load.sh
then
    echo "in tests.sh again --------"
    echo "0"
else
    echo "in tests.sh again --------"
    echo "1"
fi

$ cat ../tut0/load.sh:
Code:
#!/bin/bash
echo "in load.sh --------"
../suite/main
rc=$?
echo "rc:"
echo $rc
exit $rc

$ cat main.cpp:
Code:
#include <iostream>

int main()
{
    std::cout << "in main() -------\n";
    return 1; //EXIT_FAILURE
}

From the terminal:
Code:
$  ./tests.sh
in tests.sh --------
in load.sh --------
in main()-------
rc:
1
in tests.sh again --------
1

# 18  
Old 01-23-2017
Quote:
Originally Posted by wolfv
The following example is similar, but with load.sh script in a different directory.
Code:
#!/bin/bash
if test ../tut0/load.sh
then
    echo "0"
else
    echo "1"
fi

You don't evaluate the exit code of load.sh. For example, if you would write

Code:
if test /usr/bin/false
then
  echo yes
fi

you would also get "yes" as answer.

You should write your code as

Code:
if ../tut0/load.sh
then
  echo 0
else
  echo 1
fi

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