python with echo

Discussion in 'Python' started by hong zhang, Nov 12, 2009.

  1. hong zhang

    hong zhang Guest

    List,

    I have a question of python using echo.

    POWER = 14
    return_value = os.system('echo 14 > /sys/class/net/wlan1/device/tx_power')

    can assign 14 to tx_power

    But
    return_value = os.system('echo $POWER > /sys/class/net/wlan1/device/tx_power')

    return_value is 256 not 0. It cannot assign 14 to tx_power.

    What problem is it?

    os.system("echo $POWER") returns 0 but
    os.system("echo $POWER > /sys/class/net/wlan1/device/tx_power") returns 256.

    Appreciate any help!

    Thanks.

    ---henry
     
    hong zhang, Nov 12, 2009
    #1
    1. Advertising

  2. On Wed, 11 Nov 2009 17:24:37 -0800, hong zhang wrote:

    > List,
    >
    > I have a question of python using echo.
    >
    > POWER = 14
    > return_value = os.system('echo 14 >
    > /sys/class/net/wlan1/device/tx_power')
    >
    > can assign 14 to tx_power
    >
    > But
    > return_value = os.system('echo $POWER >
    > /sys/class/net/wlan1/device/tx_power')


    POWER = 14 doesn't create an environment variable visible to echo. It is
    a Python variable.


    >>> POWER = 14
    >>> import os
    >>> return_value = os.system('echo $POWER')


    >>> return_value

    0



    > return_value is 256 not 0. It cannot assign 14 to tx_power.



    I don't understand that. Exit status codes on all systems I'm familiar
    with are limited to 0 through 255. What operating system are you using?

    Assuming your system allows two-byte exit statuses, you should check the
    documentation for echo and the shell to see why it is returning 256.

    Have you tried this in the shell, without involving Python? I will almost
    guarantee that Python is irrelevant to the problem.




    --
    Steven
     
    Steven D'Aprano, Nov 12, 2009
    #2
    1. Advertising

  3. hong zhang schrieb:
    > List,
    >
    > I have a question of python using echo.
    >
    > POWER = 14
    > return_value = os.system('echo 14 > /sys/class/net/wlan1/device/tx_power')
    >
    > can assign 14 to tx_power
    >
    > But
    > return_value = os.system('echo $POWER > /sys/class/net/wlan1/device/tx_power')
    >
    > return_value is 256 not 0. It cannot assign 14 to tx_power.


    Because $POWER is an environment-variable, but POWER is a
    python-variable, which isn't magically becoming an environment variable.

    There are various ways to achieve what you want, but IMHO you should
    ditch the whole echo-business as it's just needless in python itself:

    with open("/sys/class/net/wlan1/device/tx_power", "w") as f:
    f.write("%i" % POWER)

    Diez
     
    Diez B. Roggisch, Nov 12, 2009
    #3
  4. hong zhang

    MRAB Guest

    Steven D'Aprano wrote:
    > On Wed, 11 Nov 2009 17:24:37 -0800, hong zhang wrote:
    >
    >> List,
    >>
    >> I have a question of python using echo.
    >>
    >> POWER = 14
    >> return_value = os.system('echo 14 >
    >> /sys/class/net/wlan1/device/tx_power')
    >>
    >> can assign 14 to tx_power
    >>
    >> But
    >> return_value = os.system('echo $POWER >
    >> /sys/class/net/wlan1/device/tx_power')

    >
    > POWER = 14 doesn't create an environment variable visible to echo. It is
    > a Python variable.
    >
    >
    >>>> POWER = 14
    >>>> import os
    >>>> return_value = os.system('echo $POWER')

    >
    >>>> return_value

    > 0
    >
    >
    >
    >> return_value is 256 not 0. It cannot assign 14 to tx_power.

    >
    >
    > I don't understand that. Exit status codes on all systems I'm familiar
    > with are limited to 0 through 255. What operating system are you using?
    >
    > Assuming your system allows two-byte exit statuses, you should check the
    > documentation for echo and the shell to see why it is returning 256.
    >

    In some OSs the exit status consists of 2 fields, one being the child
    process's exit status and the other being supplied by the OS.

    The reason is simple. What if the child process terminated abnormally?
    You'd like an exit status to tell you that, but you wouldn't want it to
    be confused with the child process's own exit status, assuming that it
    had terminated normally.

    > Have you tried this in the shell, without involving Python? I will almost
    > guarantee that Python is irrelevant to the problem.
    >
     
    MRAB, Nov 12, 2009
    #4
  5. hong zhang

    Hans Mulder Guest

    Steven D'Aprano wrote:
    > On Wed, 11 Nov 2009 17:24:37 -0800, hong zhang wrote:
    >
    >> List,
    >>
    >> I have a question of python using echo.
    >>
    >> POWER = 14
    >> return_value = os.system('echo 14 >
    >> /sys/class/net/wlan1/device/tx_power')
    >>
    >> can assign 14 to tx_power
    >>
    >> But
    >> return_value = os.system('echo $POWER >
    >> /sys/class/net/wlan1/device/tx_power')

    >
    > POWER = 14 doesn't create an environment variable visible to echo. It is
    > a Python variable.
    >
    >>>> POWER = 14
    >>>> import os
    >>>> return_value = os.system('echo $POWER')

    >
    >>>> return_value

    > 0


    You can set environment variables from within Python using os.putenv:

    >>> import os
    >>> os.putenv('POWER', '14')
    >>> return_value = os.system('echo $POWER')

    14
    >>> return_value

    0

    Keep in mind that putenv() only affects processes started by Python
    after you call putenv. It does not, for example, affect the shell
    process you used to invoke Python:

    $ POWER=14
    $ python -c 'import os
    os.putenv("POWER", "42")
    os.system("echo $POWER")'
    42
    $ echo $POWER
    14
    $

    >> return_value is 256 not 0. It cannot assign 14 to tx_power.

    >
    > I don't understand that. Exit status codes on all systems I'm familiar
    > with are limited to 0 through 255. What operating system are you using?


    Probably some form of Unix. The value returned by os.system() is the
    exit status shifted left one byte, for example:

    >>> os.system("exit 1")

    256

    Hope this helps,

    -- HansM
     
    Hans Mulder, Nov 12, 2009
    #5
  6. On Thu, 12 Nov 2009 19:30:01 +0000, MRAB wrote:

    >> I don't understand that. Exit status codes on all systems I'm familiar
    >> with are limited to 0 through 255. What operating system are you using?
    >>
    >> Assuming your system allows two-byte exit statuses, you should check
    >> the documentation for echo and the shell to see why it is returning
    >> 256.
    >>

    > In some OSs the exit status consists of 2 fields, one being the child
    > process's exit status and the other being supplied by the OS.


    Which OSes?

    > The reason is simple. What if the child process terminated abnormally?
    > You'd like an exit status to tell you that,


    Which it does. Anything other than 0 is an error. I see that, for
    example, if I interrupt "sleep 30" with ctrl-C instead of waiting for it
    to exit normally, it returns with an exit status of 130.

    [steve@soy ~]$ sleep 3 # no interrupt
    [steve@soy ~]$ echo $?
    0
    [steve@soy ~]$ sleep 3 # interrupt with ctrl-C

    [steve@soy ~]$ echo $?
    130

    I get the same result on a Linux box and a Solaris box, both running bash.



    > but you wouldn't want it to
    > be confused with the child process's own exit status, assuming that it
    > had terminated normally.


    I don't understand what you mean here. Why are you assuming it terminated
    normally if it terminated abnormally?




    --
    Steven
     
    Steven D'Aprano, Nov 13, 2009
    #6
  7. hong zhang

    MRAB Guest

    Steven D'Aprano wrote:
    > On Thu, 12 Nov 2009 19:30:01 +0000, MRAB wrote:
    >
    >>> I don't understand that. Exit status codes on all systems I'm familiar
    >>> with are limited to 0 through 255. What operating system are you using?
    >>>
    >>> Assuming your system allows two-byte exit statuses, you should check
    >>> the documentation for echo and the shell to see why it is returning
    >>> 256.
    >>>

    >> In some OSs the exit status consists of 2 fields, one being the child
    >> process's exit status and the other being supplied by the OS.

    >
    > Which OSes?
    >
    >> The reason is simple. What if the child process terminated abnormally?
    >> You'd like an exit status to tell you that,

    >
    > Which it does. Anything other than 0 is an error. I see that, for
    > example, if I interrupt "sleep 30" with ctrl-C instead of waiting for it
    > to exit normally, it returns with an exit status of 130.
    >
    > [steve@soy ~]$ sleep 3 # no interrupt
    > [steve@soy ~]$ echo $?
    > 0
    > [steve@soy ~]$ sleep 3 # interrupt with ctrl-C
    >
    > [steve@soy ~]$ echo $?
    > 130
    >
    > I get the same result on a Linux box and a Solaris box, both running bash.
    >
    >
    >
    >> but you wouldn't want it to
    >> be confused with the child process's own exit status, assuming that it
    >> had terminated normally.

    >
    > I don't understand what you mean here. Why are you assuming it terminated
    > normally if it terminated abnormally?
    >

    You want to be able to distinguish between a child process terminating
    with an exit status, and failing to run a child process for some reason.
     
    MRAB, Nov 13, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leszek Dubiel
    Replies:
    3
    Views:
    9,948
  2. Guest

    echo user input

    Guest, May 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    489
    William F. Robertson, Jr.
    May 27, 2005
  3. =?Utf-8?B?Q3JhaWc=?=

    echo for a session variable

    =?Utf-8?B?Q3JhaWc=?=, Aug 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    367
  4. Josef Garvi

    Re: echo servers

    Josef Garvi, Aug 3, 2003, in forum: Java
    Replies:
    0
    Views:
    420
    Josef Garvi
    Aug 3, 2003
  5. kj
    Replies:
    7
    Views:
    717
Loading...

Share This Page