LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   returned code (https://www.linuxquestions.org/questions/programming-9/returned-code-663906/)

tostay2003 08-19-2008 04:00 PM

returned code
 
I have two scripts
Script1.sh
Code:

#!/bin/ksh
help()
{
 print "abcdefghi..."
 exit 1
}
help

Script2.sh
Code:

#!/bin/ksh
./Script1.sh
RC=$?
echo "Returned Code $RC"

I always get the returned code as 0, where as I am expecting 1 as i have issued exit 1. I do get "abcdefghi...", which means the help function was getting executed.

keratos 08-19-2008 04:06 PM

change exit to return

and after you call help you need:

exit $?

Mr. C. 08-19-2008 04:08 PM

It is best to show results like this:
Code:

$ ls -l Script*
-rwxr-xr-x  1 mrc wheel  58 Aug 19 14:05 Script1.sh*
-rwxr-xr-x  1 mrc wheel  56 Aug 19 14:05 Script2.sh*

$ cat Script1.sh
#!/bin/ksh

help()
{
 print "abcdefghi..."
 exit 1
}
help

$ cat Script2.sh
#!/bin/ksh

./Script1.sh
RC=$?
echo "Returned Code $RC"

$ ./Script2.sh
abcdefghi...
Returned Code 1

As you can see, it works as expected. Show your versions as I did.

Mr. C. 08-19-2008 04:09 PM

Quote:

Originally Posted by keratos (Post 3253034)
change exit to return

and after you call help you need:

exit $?

There is nothing wrong with exit'ing from within a function.

tostay2003 08-19-2008 04:13 PM

Quote:

Originally Posted by keratos (Post 3253034)
change exit to return

and after you call help you need:

exit $?


Script1.sh
Code:

#!/bin/ksh
help()
{
 print "abcdefghi..."
 return 1
}
help1()
{
 return 1
}
help2()
{
 return 1
}
help
help1
help2

Script2.sh
Code:

#!/bin/ksh
./Script1.sh
RC=$?
echo "Returned Code $RC"

How shall I go about when, there are more than 1 function being called as shown above

Quote:

help
help1
help2
I want to stop the execution if function - help returns code 1, similarly with help1, help2. Do I need to always verify the return of the function and only then able to decide whether to exit the script or progress further.

keratos 08-19-2008 04:16 PM

you didnt say that in your 1st post.

tostay2003 08-19-2008 04:25 PM

Quote:

Originally Posted by keratos (Post 3253052)
you didnt say that in your 1st post.

Tried to simply and forgot to mention it in the first post

Mr. C. 08-19-2008 04:28 PM

I think your intent was clear from the code!

tostay2003 08-19-2008 04:41 PM

Quote:

Originally Posted by Mr. C. (Post 3253038)
It is best to show results like this:
Code:

$ ls -l Script*
-rwxr-xr-x  1 mrc wheel  58 Aug 19 14:05 Script1.sh*
-rwxr-xr-x  1 mrc wheel  56 Aug 19 14:05 Script2.sh*

$ cat Script1.sh
#!/bin/ksh

help()
{
 print "abcdefghi..."
 exit 1
}
help

$ cat Script2.sh
#!/bin/ksh

./Script1.sh
RC=$?
echo "Returned Code $RC"

$ ./Script2.sh
abcdefghi...
Returned Code 1

As you can see, it works as expected. Show your versions as I did.

You are right its working for me as well.

Its tough to post the script over here (company's policy). But the code is straight forward.

Code:

getFile () {
    if [ -z "$sFile"  ]
        then
          print "Cannot find File : $File"
          exit 1
      fi

    echo "File: $File"
}

I see the message
Quote:

Cannot find File : xyz
. So from the second script I am expecting RC=1, but it shows up as 0. There are no loops or any other if statements in this function.

keratos 08-19-2008 04:44 PM

Quote:

Originally Posted by Mr. C. (Post 3253070)
I think your intent was clear from the code!

ah-ha we have been blessed with a being of superior mental intellect akin to telepathy

perhaps you can close this thread by completing the questions before he asks them

this is all basic stuff that, if you RTFM, can be easily, easily resolved. Just try a little.

bye

Mr. C. 08-19-2008 04:50 PM

Understood about your companies policy.

Try set -x before the print, to see xtrace output. We've verified ksh works; now the goal is to see why your scripts are not behaving as you expect.

tostay2003 08-19-2008 04:55 PM

Quote:

Originally Posted by Mr. C. (Post 3253093)
Understood about your companies policy.

Try set -x before the print, to see xtrace output. We've verified ksh works; now the goal is to see why your scripts are not behaving as you expect.

Ran after

Code:

set -x
But it doesn't give details of how script is executed. Just display's the initial command and the log generated out of script.

Mr. C. 08-19-2008 05:04 PM

For the results to occur that you are seeing, some command MUST be running immediately after the exit from the second script - and returns a 0 status - and before you test the exit status code.

Sorry, in ksh, you use typeset -ft to trace functions:

help()
{
...
}
typeset -ft help

tostay2003 08-19-2008 05:12 PM

Quote:

typeset -ft help
I followed it on main & getFile functions and found the result as
Code:

+ sed s%/H21%%
+ cut -f1 -d:
File=
+ [ -z  ]
+ print Cannot find File : H21
Cannot find File : H21
+ exit 1

Looks like nothing is running after the exit code


Quote:

Originally Posted by Mr. C. (Post 3253114)
For the results to occur that you are seeing, some command MUST be running immediately after the exit from the second script - and returns a 0 status - and before you test the exit status code.

I have the code something like this

Code:

Function1 () {
}
Function2 () {
}
getFile () {
    if [ -z "$sFile"  ]
        then
          print "Cannot find File : $File"
          exit 1
      fi

    echo "File: $File"
}

Function3 ()
{
}

main () {

Function1

Function2

getFile

Function3

}

#setting variables here

main $* | tee $LogFile

Is anything in here causing any code to be executed after the exit statement.

I do see that functions only till getFile are executed (based on the log)

Mr. C. 08-19-2008 05:24 PM

The code of interest is the code that sets up the call to the script above and the test for its exit status. We know the script above exits correctly with exit code 1. You can verify this with strace, by placing strace in front of the call in your other script to the script above. strace will show you the exit value.

See my edit above for setting -x on functions in ksh.


All times are GMT -5. The time now is 03:45 PM.