Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python with echo

Reply
Thread Tools

python with echo

 
 
hong zhang
Guest
Posts: n/a
 
      11-12-2009
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



 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-12-2009
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
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-12-2009
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
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      11-12-2009
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.
>


 
Reply With Quote
 
Hans Mulder
Guest
Posts: n/a
 
      11-12-2009
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

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-13-2009
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
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      11-13-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
annoying CL echo in interactive python / ipython kj Python 7 10-20-2010 02:07 PM
Overloaded "echo" command in perl within system (backtick, exec) call Leszek Dubiel Perl 3 05-12-2004 11:43 AM
Basic question: Pix & ICMP echo replies Jesper Jenssen Cisco 3 11-21-2003 11:09 PM
echo echo echo craig judd Computer Support 1 09-23-2003 08:53 PM
Can't echo to router /dev/ttyS0 after reload/quit! BOK Cisco 0 07-27-2003 08:49 AM



Advertisments