Python And Bash Communicate?


Recommended Posts

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

Link to post
Share on other sites

the exit status of the bash command? if so this does not look right

shane@mainbox shane $ /sbin/ifconfig | grep essi
shane@mainbox shane $ echo $?
1

shouldn't the exit codes be the same?

>>> ifconfig = os.system('/sbin/ifconfig | grep essi')
>>> print ifconfig
256

Link to post
Share on other sites

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 by jcl
Link to post
Share on other sites

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.

Link to post
Share on other sites
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

Link to post
Share on other sites

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 by jcl
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...