Python, equivalent of set command

skip at pobox.com skip at pobox.com
Mon Mar 27 15:40:55 CEST 2006


    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



More information about the Python-list mailing list