473,394 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 34253
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 (like with the tail -f commande on Linux) I...
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 file, which is too slow. I tried using DDE but...
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 decide which of the 3 scripts to run. Now, because...
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 file that contains SQL, and some Postgres-specific...
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> int main(void)
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 not be run until a few minutes after running the...
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 code is 3. But in the python script its 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 $diff2=system("sh diff1.sh"); I need exit status of...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.