472,328 Members | 1,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

return a value to shell script

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
4 34050
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Yann.K | last post by:
Hello. Using Tkinter, i would create a widget which display a shell command return. This return is long, and i would display a real time display...
17
by: Freeserve | last post by:
Hi, I am trying to pass a value from an ASP script to a VB application running on the server. The only way I have got this to work is by using a...
3
by: D.P. Roberts | last post by:
I have 3 vbscripts and a vb form with radio buttons corresponding to each script. The form's only purpose is to provide a nice GUI for the user to...
0
by: David Roche | last post by:
Hello, I searched all through Google Groups, Google, and the Postgres docs, but to no avail. I hope someone can help me out here! I have a...
13
by: vashwath | last post by:
Hi all, To test the return value of system I wrote 2 programs which are shown below. Program 1: #include <stdio.h> #include <stdlib.h> ...
2
by: | last post by:
Hello, everybody. I want to call any shell command from php script using shell_exec(). The problem is that the next line of the php script would...
0
by: Marco Bizzarri | last post by:
On Wed, Nov 12, 2008 at 2:06 PM, devi thapa <devi.thapa@gmail.comwrote: import sys sys.exit(123) -- Marco Bizzarri
3
by: devi thapa | last post by:
Hi, I am running one service in the python script eg like "service httpd status". If I execute this command in normal shell kernel, the return...
4
by: babp | last post by:
HI , Is there any way to return a value of variable from shell to perl script. Code: === Perl file my $diff1=system("sh diff.sh"); my...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.