I would like to run a linux command from within a python script
Code:
#!usr/bin/python |
It would actually help to see which error you get, don't you think?
May I also ask why you start a simple script with root privileges? |
Quote:
I can type in a command, such as ls or ifconfig, and it works (displays the proper output)...however, the last line (after the output) says sh: Syntax error: ";" unexpected. Thanks! |
Just for clarification: sudo is only needed (and should only be used) when an action needs root privileges. Using it in a different case is simply unnecessary and risky.
Please post the exact output on the screen from the line you start the script to the error message, so that we can get a better understanding of the error. |
1 Attachment(s)
Quote:
|
According to online Python documentation for module "commands," you need to supply the actual command in your call to getoutput() (see the examples at the bottom of the page linked).
As an additional note, as the top of the page indicates, the "commands" module has been listed as deprecated since version 2.6. The replacement module "subprocess" was added in version 2.4. I would suggest using the subprocess module now, even for simple things. As an example, here's a small script I used a while back demonstrating the subprocess module's Popen call (which is basically what you are trying to do). The script calculates the rows and columns of the current terminal by invoking the external stty command. Code:
#!/usr/bin/python |
Quote:
|
Quote:
Python doesn't care whether you give it a literal string or a string stored in a variable. Here's the script using a command-through-variable approach: Code:
#!/usr/bin/python As a follow-up in case a I misunderstood your question... the lines in green from the code above are output processing statements. You would replace those with whatever processing (or printing) statements your script needs--similar to how you might process the contents of a file line by line. |
All times are GMT -5. The time now is 04:20 AM. |