I would like to run a linux command from within a python script
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would like to run a linux command from within a python script
Code:
#!usr/bin/python
import os, commands
pattern = raw_input('Enter a command you would like to run: ')
os.system(pattern)
output = commands.getoutput('')
print output
For some reason, I am getting an error when I do sudo python runthisscript.py ...i think it resides in my output = commands. section...any suggestions?
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?
Running with sudo is just the way I was taught...My mistake..When running the program the first line says, "Enter a command you would like to run:"
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.
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.
Running with sudo is just the way I was taught...My mistake..When running the program the first line says, "Enter a command you would like to run:"
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!
Here is the screen shot...my code is exactly how it appears in my original post.
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.
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.
Thanks for the help. However, what if I didn't want to specify the command in the script itself but rather while I am running it? How would I go about restructuring this script to make it work that way.
EDIT:
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.
Last edited by Dark_Helmet; 02-28-2012 at 03:37 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.