Python code to replace shell scripts

Discussion in 'Python' started by Daven Nair, Dec 11, 2003.

  1. Daven Nair

    Daven Nair Guest

    Hi,

    I would like to know if Python supports codes similar to shell scripts:

    count=`ps -ef|grep "pattern"|wc -l`
    for count in `echo $count`
    do
    done
    fi

    Can I export a variable say var from os.system("var=`ps -ef|grep pattern|wc
    -l`")


    thanks

    _________________________________________________________________
    Cell phone ‘switch’ rules are taking effect — find out more here.
    http://special.msn.com/msnbc/consumeradvocate.armx
    Daven Nair, Dec 11, 2003
    #1
    1. Advertising

  2. "Daven Nair" <> writes:

    >
    > I would like to know if Python supports codes similar to shell scripts:
    >
    > count=`ps -ef|grep "pattern"|wc -l`
    > for count in `echo $count`
    > do
    > done
    > fi


    See 'pydoc commands'. You could probably do with

    pattern = "<grep pattern>"
    cmd_status, count = commands.getstatusoutput("ps -ef|grep -c %s" % pattern)

    for i in range(count):
    # do something count times

    Note: you don't need wc -l after grep, because grep has '-c' switch.

    Note2: not tested, some typos etc may be present.

    --
    # Edvard Majakari Software Engineer
    # PGP PUBLIC KEY available Soli Deo Gloria!

    $_ = '456476617264204d616a616b6172692c20612043687269737469616e20'; print
    join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";
    Edvard Majakari, Dec 11, 2003
    #2
    1. Advertising

  3. In article <>,
    Daven Nair <> wrote:
    >Hi,
    >
    >I would like to know if Python supports codes similar to shell scripts:
    >
    >count=`ps -ef|grep "pattern"|wc -l`
    >for count in `echo $count`
    > do
    > done
    >fi
    >
    >Can I export a variable say var from os.system("var=`ps -ef|grep pattern|wc
    >-l`")

    .
    .
    .
    While I don't understand your questions, I suspect the series I've launched
    with <URL: http://www.samag.com/documents/s=8964/sam0312a/0312a.htm > will
    interest you.

    When you write, "Can I export a variable ...", are you asking that the
    result of an external process be bound to a Python name (or variable), OR
    that a Python process export a "shell" variable to its environment, OR
    ....?
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
    Cameron Laird, Dec 11, 2003
    #3
  4. Daven Nair

    William Park Guest

    Daven Nair <> wrote:
    > Hi,
    >
    > I would like to know if Python supports codes similar to shell scripts:
    >
    > count=`ps -ef|grep "pattern"|wc -l`
    > for count in `echo $count`
    > do
    > done
    > fi
    >
    > Can I export a variable say var from os.system("var=`ps -ef|grep pattern|wc
    > -l`")


    No. 'os.system()' will fork a subshell, and, as you know, subshell
    cannot change parent's environment. Furthermore, your shell script is
    not proper. It should go like
    for count in `...`; do
    ...
    done

    In any case, although Python does something well, shell does most things
    better. (It's okey... I've got my helmet on.)

    --
    William Park, Open Geometry Consulting, <>
    Linux solution for data management and processing.
    William Park, Dec 12, 2003
    #4
  5. Daven Nair fed this fish to the penguins on Thursday 11 December 2003
    10:30 am:

    > I would like to know if Python supports codes similar to shell
    > scripts:
    >
    > count=`ps -ef|grep "pattern"|wc -l`
    > for count in `echo $count`


    I suspect you would have to separate the `...` items, using something
    like one of the popen() family, and capture the results, then process
    those same results.

    I'll speak blasphemy here (either that, or Tower of Babel polyglot <G>)

    (o)REXX would be a bit more transparent (though I don't think IBM
    makes it free to all -- you can get an "evaluation" copy for Linux). In
    REXX, anything line that is not recognized as a REXX statement is
    automatically passed to the current command processor (normally the
    shell -- though the Amiga really took advantage of the ability to
    change "command processor" making AREXX a scripting language for any
    application that created an "AREXX port").

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Bestiaria Home Page: http://www.beastie.dm.net/ <
    > Home Page: http://www.dm.net/~wulfraed/ <
    Dennis Lee Bieber, Dec 12, 2003
    #5
  6. Daven Nair

    Alan Gauld Guest

    On 12 Dec 2003 01:58:23 GMT, William Park <>
    wrote:

    > > count=`ps -ef|grep "pattern"|wc -l`
    > > for count in `echo $count`
    > > do
    > > done
    > > fi


    In this case you could change the style to a more slightly
    more pythonic approach:

    print len(os.popen('ps -ef|grep "pattern"').read().split())

    > In any case, although Python does something well, shell does most things
    > better. (It's okey... I've got my helmet on.)


    Shell is generally better at quickly gluing together existing
    commands. But does so at a significant cost in machine resources
    and often execution time. Python provides a different approach
    that is generally better where the solution must be repeated
    often or where no suitable set of commands already exists.

    Alan G.
    Author of the Learn to Program website
    http://www.freenetpages.co.uk/hp/alan.gauld
    Alan Gauld, Dec 12, 2003
    #6
  7. In article <>,
    Dennis Lee Bieber <> wrote:
    .
    .
    .
    > (o)REXX would be a bit more transparent (though I don't think IBM
    >makes it free to all -- you can get an "evaluation" copy for Linux). In

    .
    .
    .
    There *are* open-source REXXs (even a mod_rexx!) which are
    alternatives to IBM's. Normally, at this point, I'd provide
    references to a couple; a combination of catastrophes seems
    to be afflicting my REXX-related notes, though, and I have
    none at hand that satisfy me. I'll cross-post to c.l.r for
    the benefit of those who want to pursue this.
    --

    Cameron Laird <>
    Business: http://www.Phaseit.net
    Cameron Laird, Dec 12, 2003
    #7
  8. Daven Nair

    William Park Guest

    Alan Gauld <> wrote:
    > On 12 Dec 2003 01:58:23 GMT, William Park <>
    > wrote:
    > > In any case, although Python does something well, shell does most
    > > things better. (It's okey... I've got my helmet on.)

    >
    > Shell is generally better at quickly gluing together existing
    > commands. But does so at a significant cost in machine resources and
    > often execution time. Python provides a different approach that is
    > generally better where the solution must be repeated often or where no
    > suitable set of commands already exists.


    That is true, until you learn and program in shell.

    --
    William Park, Open Geometry Consulting, <>
    Linux solution for data management and processing.
    William Park, Dec 12, 2003
    #8
  9. Daven Nair

    Serge Orlov Guest

    "William Park" <> wrote in message news:brd7nv$264kc$-berlin.de...
    > Alan Gauld <> wrote:
    > > On 12 Dec 2003 01:58:23 GMT, William Park <>
    > > wrote:
    > > > In any case, although Python does something well, shell does most
    > > > things better. (It's okey... I've got my helmet on.)

    > >
    > > Shell is generally better at quickly gluing together existing
    > > commands. But does so at a significant cost in machine resources and
    > > often execution time. Python provides a different approach that is
    > > generally better where the solution must be repeated often or where no
    > > suitable set of commands already exists.

    >
    > That is true, until you learn and program in shell.


    And that will be true again once you have handy module for gluing
    together cli applications. Since Python is not popular among
    system administrators, nobody was bothered to do it (yet?)
    Serge Orlov, Dec 12, 2003
    #9
  10. Daven Nair

    Alan Gauld Guest

    On 12 Dec 2003 20:17:04 GMT, William Park <>
    wrote:
    > > often execution time. Python provides a different approach that is
    > > generally better where the solution must be repeated often or where no
    > > suitable set of commands already exists.

    >
    > That is true, until you learn and program in shell.


    Well I've been programming Bourne and Korn shells for about 15
    years now. But I still pick python for anything that needs a GUI
    or has to run as a daemon or does heavy network calls. I'd also
    use Python if I had to write a Web Browser or Word Processor or
    Programming/Test environment.

    In fact anything that needs more than a few hundred lines of
    code. Shell is great for what its good at but orders of magnitude
    slower and more resource hungry than Python for complex tasks.
    Just think about how many processes get launched, the inefficient
    text parsing, the nested shells etc. And as for data structure
    support!

    For sys admin type tasks, Shell is great, for applications its a
    forced fit.

    Alan g
    Author of the Learn to Program website
    http://www.freenetpages.co.uk/hp/alan.gauld
    Alan Gauld, Dec 13, 2003
    #10
  11. [note follow-ups set to comp.lang.rexx]

    In article <>,
    Cameron Laird <> wrote:

    % There *are* open-source REXXs (even a mod_rexx!) which are
    % alternatives to IBM's. Normally, at this point, I'd provide
    % references to a couple;

    Start with http://www.rexxla.org and look at the links page. I think
    every implementation is referred to either there or at Cowlishaw's
    rexx page.
    --

    Patrick TJ McPhee
    East York Canada
    Patrick TJ McPhee, Dec 14, 2003
    #11
    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. Ryan Gaffuri
    Replies:
    2
    Views:
    435
    Default User
    Oct 22, 2003
  2. Nuff Said
    Replies:
    9
    Views:
    694
    Nuff Said
    Apr 30, 2004
  3. Jan Danielsson
    Replies:
    8
    Views:
    620
    Mike Meyer
    Jul 22, 2005
  4. Jp Calderone
    Replies:
    0
    Views:
    449
    Jp Calderone
    Jul 21, 2005
  5. davidj411
    Replies:
    0
    Views:
    492
    davidj411
    Jun 27, 2008
Loading...

Share This Page