Re: Linux shell to python

Discussion in 'Python' started by Chris Angelico, Jul 30, 2012.

  1. On Mon, Jul 30, 2012 at 5:05 PM, Vikas Kumar Choudhary
    <> wrote:
    >
    > I was trying porting from bash shell to python.
    >
    > 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'


    Welcome!

    While it's technically possible to do exactly that in Python (using
    subprocess as you describe), there's usually a more efficient and
    cleaner method of achieving the same goal. With a port such as you
    describe, it's probably best to go right back to the conceptual level
    and work out what exactly you're trying to do, and then look at
    implementing that in Python. You'll end up with much cleaner code at
    the end of it.

    For an initial guess, I would say that you'll use subprocess to invoke
    lspci, but then everything else will be done in Python directly.

    ChrisA
     
    Chris Angelico, Jul 30, 2012
    #1
    1. Advertising

  2. you can use commands.getstatusoutput(command), the shell command special charactor (like "$ and so on )should be escaped!


    在 2012å¹´7月30日星期一UTC+8下åˆ3æ—¶40分04秒,Chris Angelico写é“:
    > On Mon, Jul 30, 2012 at 5:05 PM, Vikas Kumar Choudhary
    >
    > <> wrote:
    >
    > >

    >
    > > I was trying porting from bash shell to python.

    >
    > >

    >
    > > 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'

    >
    >
    >
    > Welcome!
    >
    >
    >
    > While it's technically possible to do exactly that in Python (using
    >
    > subprocess as you describe), there's usually a more efficient and
    >
    > cleaner method of achieving the same goal. With a port such as you
    >
    > describe, it's probably best to go right back to the conceptual level
    >
    > and work out what exactly you're trying to do, and then look at
    >
    > implementing that in Python. You'll end up with much cleaner code at
    >
    > the end of it.
    >
    >
    >
    > For an initial guess, I would say that you'll use subprocess to invoke
    >
    > lspci, but then everything else will be done in Python directly.
    >
    >
    >
    > ChrisA
     
    å¼ å°‘åŽ, Jul 30, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    5,891
    Jonathan Bromley
    Feb 18, 2005
  2. Christian Heimes
    Replies:
    0
    Views:
    630
    Christian Heimes
    Feb 27, 2008
  3. Gerardo Herzig
    Replies:
    1
    Views:
    1,141
    Philipp Pagel
    Feb 27, 2008
  4. D'Arcy J.M. Cain
    Replies:
    0
    Views:
    893
    D'Arcy J.M. Cain
    Feb 27, 2008
  5. edwithad

    Bash shell to Python shell?

    edwithad, Sep 28, 2009, in forum: Python
    Replies:
    4
    Views:
    1,811
Loading...

Share This Page