Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Linux shell to python

Reply
Thread Tools

Re: Linux shell to python

 
 
Peter Otten
Guest
Posts: n/a
 
      07-30-2012
Vikas Kumar Choudhary wrote:


> let me know if someone has tried to implement (grep and PIPE) shell
> commands in python `lspci | grep Q | grep "$isp_str1" | grep "$isp_str2"
> | cut -c1-7'
>
> I tried to use python subprocess and OS.Popen modules.


subprocess is the way to go.

> I was trying porting from bash shell to python.


Here's an example showing how to translate a shell pipe:

http://docs.python.org/library/subpr...shell-pipeline

But even if you can port the shell script literally I recommend a more
structured approach:

import subprocess
import itertools

def parse_data(lines):
for not_empty, group in itertools.groupby(lines, key=bool):
if not_empty:
triples = (line.partition(":") for line in group)
pairs = ((left, right.strip()) for left, sep, right in triples)
yield dict(pairs)

if __name__ == "__main__":
def get(field):
return entry.get(field, "").lower()
output = subprocess.Popen(["lspci", "-vmm"],
stdout=subprocess.PIPE).communicate()[0]
for entry in parse_data(output.splitlines()):
if "nvidia" in get("Vendor") and "usb" in get("Device"):
print entry["Slot"]
print entry["Device"]
print


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell to Python shell? edwithad Python 4 09-28-2009 07:56 AM
Re: How to pass shell variable to shell script from python D'Arcy J.M. Cain Python 0 02-27-2008 01:56 PM
Re: How to pass shell variable to shell script from python Gerardo Herzig Python 1 02-27-2008 12:19 PM
Re: How to pass shell variable to shell script from python Christian Heimes Python 0 02-27-2008 10:53 AM
can I run unix shell command in the ModelSim shell? clinton__bill@hotmail.com VHDL 2 02-18-2005 10:04 PM



Advertisments