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.
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:
#!/bin/bash
if test ./load.sh ; then
echo "0"
else
echo "1"
fi
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:
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:
#!/bin/bash
main
rc=$?
if [ $rc -eq 0 ]
then exit 0
else exit $rc
fi
or:
#!/bin/bash
main
rc=$?
if [ $rc -eq 0 ]
then exit 0
fi
exit $rc
In your modified example, I don't see that you would execute load.sh anywhere. What effect would you like to see? Please provide a complete transscript, showing what you are doing.
The example in post #9 works, it has all the files in one directory.
A C++ function main(), returns an error code (1 means EXIT_FAILURE).
The error code is passed from the main() function, to load.sh, to test.sh.
And then test.sh uses the returned error code in a conditional statement.
The following example is similar, but with load.sh script in a different directory.
The files where copied from the example in post #9, and the relative paths updated.
But the output is not as expected, and Bash is not printing any error messages.
The following is a listing of the files with the new relative paths, followed by output.
$ cat tests.sh with new relative path:
#!/bin/bash
if test ../tut0/load.sh
then
echo "0"
else
echo "1"
fi
$ cat ../tut0/load.sh with new relative path:
#!/bin/bash
../suite/main
rc=$?
echo $rc
exit $rc
$ cat main.cpp:
int main()
{
return 1; //EXIT_FAILURE
}
From the terminal:
$ g++ main.cpp -o main
$ ./tests.sh
0
I was expecting it to output "1" twice, as in post #9.
A top view of the directory structure:
$ cd ..
$ ls
suite tut0 tut1
$ ls suite
load.sh main main.cpp~ tests.sh
load.sh~ main.cpp tests.sh~
$ ls tut0
load.sh load.sh~
What is the proper way to specify a relative path in bash?
Good idea. The pwd and ls outputs are as expected. But load.sh never gets called.
Why is load.sh not called?
$ cat tests.sh
#!/bin/bash
echo "in tests.sh --------"
echo "pwd:"
pwd
echo "ls:"
ls
echo "ls ../tut0:"
ls ../tut0
echo "load.sh returned:"
if test ../tut0/load.sh
then
echo "0"
else
echo "1"
fi
$ cat ../tut0/load.sh
#!/bin/bash
echo "in load.sh --------"
echo "pwd:"
pwd
echo "ls:"
ls
ls "ls ../suite:"
ls ../suite
../suite/main
rc=$?
echo "rc:"
echo $rc
exit $rc
From the terminal:
$ ./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~
load.sh returned:
0
I was expecting output to also contain the line "in load.sh --------".
That fixed it!
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:
#!/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