Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38737

Re: call from pthon to shell

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <andrew3@r3dsolutions.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.025
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'output': 0.04; 'bash': 0.07; '"-c",': 0.16; '(exit': 0.16; 'echo': 0.16; 'first:': 0.16; 'subprocess': 0.16; 'wrote:': 0.17; 'shell': 0.18; '>>>': 0.18; 'import': 0.21; '&gt;&gt;&gt;': 0.22; 'defined': 0.22; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'expansion': 0.27; 'in.': 0.27; 'hopefully': 0.33; 'like:': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:phx3.secureserver.net': 0.62; 'received:prod.phx3.secureserver.net': 0.62; 'subject: ': 0.66; 'header:Reply-To:1': 0.68; 'received:173.201': 0.91; 'received:173.201.192': 0.91
Date Tue, 12 Feb 2013 01:00:38 +0000
From Andrew Robinson <andrew3@r3dsolutions.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120909 Thunderbird/15.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: call from pthon to shell
References <1360647500.45284.YahooMailNeo@web160602.mail.bf1.yahoo.com>
In-Reply-To <1360647500.45284.YahooMailNeo@web160602.mail.bf1.yahoo.com>
Content-Type multipart/alternative; boundary="------------040209090902050702030802"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To Andrew Robinson <andrew3@r3dsolutions.com>
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1693.1360659774.2939.python-list@python.org> (permalink)
Lines 106
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360659774 news.xs4all.nl 6867 [2001:888:2000:d::a6]:50354
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38737

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 02/12/2013 05:38 AM, Bqsj Sjbq wrote:
> >>> import os
> >>> os.system("i=3")
> 0
> >>> os.system("echo $i")
>
> 0
>
> why i can not get the value of i?
>
>
First:
os.system is only defined to give the return value (exit code) of the 
sub-process.

However, one way to get the output of shell commands is to use subprocess.

import subprocess
x = subprocess.check_output( [ "echo", "3,5,7" ] )

However, bash built-ins are not executables; nor is shell expansion 
performed; so you will actually need to do something like:
x=subprocess.check_output( [ "bash", "-c", "i=3; echo $i" ] )
 >>> x
 >>> '3\n'

To get the result you're interested in.
There may be better ways to get the result you want.... but hopefully 
you understand the problem better.

:)


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: call  from pthon  to shell Andrew Robinson <andrew3@r3dsolutions.com> - 2013-02-12 01:00 +0000

csiph-web