Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

python with echo

15 views
Skip to first unread message

hong zhang

unread,
Nov 11, 2009, 8:24:37 PM11/11/09
to pytho...@python.org
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



Himanshu

unread,
Nov 11, 2009, 9:30:57 PM11/11/09
to hong zhang, pytho...@python.org
2009/11/12 hong zhang <henryz...@yahoo.com>:

Sorry cannot try it myself to confirm. Could it be that $POWER is not
set. Does it write to a regular file.

Thank You,
++imanshu

MRAB

unread,
Nov 11, 2009, 9:49:54 PM11/11/09
to Python List
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')
>
> 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.
>
Did you think that you could say:

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

in a Python script and that would do the same as:

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

That won't work because 'POWER' exists only in Python and 'echo' is
being run in the (Linux?) shell.

You could try creating the command-line and then passing it to 'system':

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

Steven D'Aprano

unread,
Nov 11, 2009, 10:07:12 PM11/11/09
to
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

Diez B. Roggisch

unread,
Nov 12, 2009, 5:41:12 AM11/12/09
to
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

MRAB

unread,
Nov 12, 2009, 2:30:01 PM11/12/09
to pytho...@python.org
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.

Hans Mulder

unread,
Nov 12, 2009, 4:20:47 PM11/12/09
to
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

Christian Heimes

unread,
Nov 12, 2009, 5:31:00 PM11/12/09
to pytho...@python.org
Diez B. Roggisch wrote:
> with open("/sys/class/net/wlan1/device/tx_power", "w") as f:
> f.write("%i" % POWER)

IIRC the sys and proc virtual file system requires new line at the end
of a modifier. At least echo appends \n unless it's told otherwise.

Christian

Steven D'Aprano

unread,
Nov 12, 2009, 8:49:08 PM11/12/09
to
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

MRAB

unread,
Nov 12, 2009, 9:22:26 PM11/12/09
to pytho...@python.org
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.
0 new messages