python with echo

H

hong zhang

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
 
S

Steven D'Aprano

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.

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.
 
D

Diez B. Roggisch

hong said:
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
 
M

MRAB

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




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.
 
H

Hans Mulder

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

0

You can set environment variables from within Python using os.putenv:
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
$
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:
256

Hope this helps,

-- HansM
 
S

Steven D'Aprano

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?
 
M

MRAB

Steven said:
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.
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top