Python, equivalent of set command

L

loial

In unix shell script I can do the following to get the status and
values returned by a unix command

OUTPUT=`some unix command`
STATUS=$?
if [ $STATUS -ne 0 ]
then
exit 1
else
set $OUTPUT
VAL1=$1
VAL2=$2
VAL3=$3
fi

How can I achieve the same in python?

I know how to run it via the os.system command and return the status,
but how do I return the values too?
 
S

skip

loial> In unix shell script I can do the following to get the status and
loial> values returned by a unix command

loial> OUTPUT=`some unix command`
loial> STATUS=$?
loial> if [ $STATUS -ne 0 ]
loial> then
loial> exit 1
loial> else
loial> set $OUTPUT
loial> VAL1=$1
loial> VAL2=$2
loial> VAL3=$3
loial> fi

loial> How can I achieve the same in python?

I'm not much of a shell programmer, but it looks like you want something
like:

import os
pipe = os.popen("some unix command")
result = pipe.read()
status = pipe.close()
if not status: # but see below...
val1, val2, val3 = result.split()

Read up on the os.popen function to get the details of what's in the status
variable. I think the exit status might be in the upper eight bits, but I
don't remember off the top of my head. That will affect the test.

Skip
 
J

Jeffrey Schwab

loial said:
In unix shell script I can do the following to get the status and
values returned by a unix command

OUTPUT=`some unix command`
STATUS=$?
if [ $STATUS -ne 0 ]
then
exit 1
else
set $OUTPUT
VAL1=$1
VAL2=$2
VAL3=$3
fi

How can I achieve the same in python?

I know how to run it via the os.system command and return the status,
but how do I return the values too?

http://docs.python.org/lib/node241.html

6.8.3.1 Replacing /bin/sh shell backquote

output=`mycmd myarg`
==>
output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]

The popen object also has a 'returncode' attribute.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,133
Latest member
364784322064

Latest Threads

Top