Passing control back to the shell script

Hi All,

I have a shell script(test_abc.sh) with the following shell commands, which are invoking the same shell script with different parameters.

test_abc.sh

. ./test.sh abc >> test.log
. ./test.sh xyz >> test.log
. ./test.sh pys >> test.log
. ./test.sh abc >> test.log
.
.

test.sh

if [ "$1" = "abc" ]; then
echo "found $1"
exit 0
else 
echo "not found $1"
exit 1 

when i execute test_abc.sh .. i only get the following in the log

found abc

My motive is that all the shell commands in test_abc.sh are run and i get the status of each of them in the test.log. But it seems like 'exit 0' and 'exit 1' are not working in my favor and are logging me out. Request your expert help here.

Thanks in advance,
Dev

Hi,

First hint:
Please close your if-construct with fi !

if expression
then
      then-commands...
else
      else-commands...
fi

maybe it works not as you suspect. But it works. The exit status is not printed, it is only set. You get your exit status with: $? And $? ist set new after every command. So if you want to get the exit status, you must read it directly after the command execution, or it is overwritten with the exit status of the next command.

Try this:

./test.sh abc >>test.log ; echo $? >>tst.log

try executing your sub-scripts in debug mode to find how the flow is working.
bash -x ./test.sh abc

exit # could be working the way it is supposed to and its a wrong choice.. read man page for exit
You may want to try continue or return # commands.

Ahh. The new answer shows what I've missed to read.

What's regarding the logout is the way you are calling your scripts:

. ./test.sh bla blub

calling a script with the dot-command(=".") is special. Dot means: don't create a new process. Run the Programm in the current process. And when you call the builtin exit in your script, your current shell is closed - as you requested by this command.

Simple solution: Don't use the dot command. Just call your script:

./test.sh bla blub
1 Like

Thanks Stomp , that was helpful :slight_smile: