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