473,721 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 34307
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.ne t | 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.ne twrote:
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.ne t | 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********@gma il.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
9925
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 think to use the text widget. I have no problem to execute the command, but how to display, in a *real-time* fashion the shell retrun?
17
4990
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 if a VB app contains a form then I can't get it to run under IIS. Anybody done something similar?
3
5655
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 the scripts are mostly identical to one another, I'd really like to combine them into one script. But I can't for the life of me figure out how to pass a radio button value from a vb form to the script so the appropriate portion of the script is...
0
452
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 slash commands. This file calls an external shell script via the backtick mechanism, and saves the value into a psql variable, for latter insertion into a table. It looks something like this:
13
7582
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
6949
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 shell command. I found the shell command couldn't be run at background mode. It seemed that the shell_exec() function waits a return from the command. Because the command doesn't return, the shell_exec() waits until timeout reached. Using the '&'...
0
1568
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
10279
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 code is different, ie not 3, I mean to say that return codes are differing. But I want the return code 3 , not the other one. So, how can I achieve this? Regards,
4
5212
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 below commands
0
8734
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9372
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9225
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9071
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5991
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3197
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2581
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2135
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.