return a value to shell script
jeff at jmcneil.net
Wed Nov 12 16:54:30 CET 2008
On Nov 12, 8:19 am, "D'Arcy J.M. Cain" <da... at druid.net> wrote:
> On Wed, 12 Nov 2008 13:09:21 +0000
> Tom Wright <te... at spam.ac.uk> wrote:
> > devi thapa wrote:
> > > I am executing a python script in a shell script. The python script
> > > actually returns a value.
> > > So, can I get the return value in a shell script? If yes, then help me
> > > out.
> > Yes. The variable $? should be bound to the return value of the last
> > foreground program to exit. The python script can return a value using
> > sys.exit(value)
> That may be true for some shells but not all. The correct answer to
> the OP's question is - check the documentation for your specific shell
> or ask on a group dedicated to the shell you are using.
> D'Arcy J.M. Cain <da... at druid.net> | Democracy is three wolveshttp://www.druid.net/darcy/ | and a sheep voting on
> +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
It depends on what your exit value is, though. While your shell will
probably provide some method for determining the exit status of your
process, you're limited to a range of 0-255. If, for example, you've
got your Python script exiting with the number of rows in a database,
you'll simply overflow.
[jeff at marvin ~]$ python -c 'import sys; sys.exit(0)' ; echo $?
[jeff at marvin ~]$ python -c 'import sys; sys.exit(1)' ; echo $?
[jeff at marvin ~]$ python -c 'import sys; sys.exit(255)' ; echo $?
[jeff at marvin ~]$ python -c 'import sys; sys.exit(256)' ; echo $?
Depending on what you're doing, printing from your Python program and
capturing standard out might be a better approach.
More information about the Python-list