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: 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; '>>>': 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 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 List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 This is a multi-part message in MIME format. --------------040209090902050702030802 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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. :) --------------040209090902050702030802 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
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.

:)


--------------040209090902050702030802--