By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

return a value to shell script

P: n/a
Hi,

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.

Regards,
Devi
Nov 12 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)

HTH
--
I'm at CAMbridge, not SPAMbridge
Nov 12 '08 #2

P: n/a
On Wed, 12 Nov 2008 13:09:21 +0000
Tom Wright <te***@spam.ac.ukwrote:
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***@druid.net | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
Nov 12 '08 #3

P: n/a
On Nov 12, 8:19 am, "D'Arcy J.M. Cain" <da...@druid.netwrote:
On Wed, 12 Nov 2008 13:09:21 +0000

Tom Wright <te...@spam.ac.ukwrote:
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...@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@marvin ~]$ python -c 'import sys; sys.exit(0)' ; echo $?
0
[jeff@marvin ~]$ python -c 'import sys; sys.exit(1)' ; echo $?
1
[jeff@marvin ~]$ python -c 'import sys; sys.exit(255)' ; echo $?
255
[jeff@marvin ~]$ python -c 'import sys; sys.exit(256)' ; echo $?
0

Depending on what you're doing, printing from your Python program and
capturing standard out might be a better approach.

HTH,

Jeff
Nov 12 '08 #4

P: n/a
On 2008-11-12, devi thapa <de********@gmail.comwrote:
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.
There are two ways to "return" something to a shell script.

1) To return a success/fail indication, use sys.exit(n). n==0
means success, n!=0 means fail.

2) To return the end value of an operation or computation to a
shell script, you write it to stdout as an ascii string.

--
Grant Edwards grante Yow! I'm wet! I'm wild!
at
visi.com
Nov 12 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.