shanenin Posted June 16, 2005 Report Share Posted June 16, 2005 I want to use the output of /sbin/ifconfig to use in a python script. In particular if essi is found in the output, I want to use that info to set a variable in my python script. I have done it this messy way below using a text file, is there a more direct way of doing this.os.system('/sbin/ifconfig | grep essi; echo $? > .ifconfig')i = open('.ifconfig', 'r')ifconfig = i.read(1)this will set the variable 'ifconfig' to '0' if essi is found, or '1' if it is not found. This is a messy way of doing it, I am sure there is a more direct way possible(witout having to use a text file). Quote Link to post Share on other sites
jcl Posted June 16, 2005 Report Share Posted June 16, 2005 The return value of os.system() is the exit status of the command.ifconfig = os.system('/sbin/ifconfig | grep essi') Quote Link to post Share on other sites
shanenin Posted June 16, 2005 Author Report Share Posted June 16, 2005 the exit status of the bash command? if so this does not look rightshane@mainbox shane $ /sbin/ifconfig | grep essishane@mainbox shane $ echo $?1shouldn't the exit codes be the same?>>> ifconfig = os.system('/sbin/ifconfig | grep essi')>>> print ifconfig256 Quote Link to post Share on other sites
jcl Posted June 16, 2005 Report Share Posted June 16, 2005 (edited) You're right. The exit status is a 16-bit value: the lower eight bits are the number of the signal that caused the process to exit or zero if it exited without a signal, and the next eight bits are the exit status. Both the 16-bit value and the second 8-bit value are called 'exit status' in the interest of confusion. It isn't Python's fault, it's replicating the behavior of the POSIX system() function.To extract the exit status (the useful one), you can use the function os.WEXITSTATUS() or shift the value eight bits right. The name 'WEXITSTATUS' is also POSIX's fault. Edited June 16, 2005 by jcl Quote Link to post Share on other sites
shanenin Posted June 16, 2005 Author Report Share Posted June 16, 2005 thanks. so long as I am checking for exit staus of zero it does not matter. The info you told me could come in very handy for an other day :-) Quote Link to post Share on other sites
jcl Posted June 16, 2005 Report Share Posted June 16, 2005 Minor issue. POSIX does not in fact specify anything about the layout of the exit status word, so you can't rely on the right shift. Whether Python uses the two-octet format on all systems I don't know. Best to stick with WEXITSTATUS. Quote Link to post Share on other sites
shanenin Posted June 16, 2005 Author Report Share Posted June 16, 2005 Minor issue. POSIX does not in fact specify anything about the layout of the exit status word, so you can't rely on the right shift. Whether Python uses the two-octet format on all systems I don't know. Best to stick with WEXITSTATUS.<{POST_SNAPBACK}>or shift the value eight bits right.I will never be a good programmer, I do not even know what that means Quote Link to post Share on other sites
jcl Posted June 16, 2005 Report Share Posted June 16, 2005 (edited) The right shift operation shifts the bits in a value so many positions right. The value 0100 right shifted by one is 0010, by two is 0001, by three is 0000. Left shift does the opposite. For added amusement, shifts come in both arithmetic and logical varieties: arithmetic shifts retain or propogate the sign bit (keeping the sign of the value the same) while logical shifts do not. Besides their pure bit-twiddling value, arithmetic shifts can be used to multiply and divide by powers of two.Python, following C, uses the operators << and >> for integer left and right shift respectively. The left shift is apparently logical (1 << 31 == -2147483648) while the right shift is arithmetic (-1 >> anything == -1) but that may be platform-specific.This should be covered somewhere in your book. It may not be introduced until later or covered in much depth because bit-twiddling is a fairly low-level operation and isn't used especially often in high-level languages. Edited June 17, 2005 by jcl Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.