Python code to replace shell scripts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daven Nair

    Python code to replace shell scripts

    Hi,

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

    count=`ps -ef|grep "pattern"|w c -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.



  • Edvard Majakari

    #2
    Re: Python code to replace shell scripts

    "Daven Nair" <nair_260@hotma il.com> writes:
    [color=blue]
    >
    > I would like to know if Python supports codes similar to shell scripts:
    >
    > count=`ps -ef|grep "pattern"|w c -l`
    > for count in `echo $count`
    > do
    > done
    > fi[/color]

    See 'pydoc commands'. You could probably do with

    pattern = "<grep pattern>"
    cmd_status, count = commands.getsta tusoutput("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!

    $_ = '45647661726420 4d616a616b61726 92c206120436872 69737469616e20' ; print
    join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60 281449,'es'),2, 4),"\n";

    Comment

    • Cameron Laird

      #3
      Re: Python code to replace shell scripts

      In article <mailman.81.107 1167444.9307.py thon-list@python.org >,
      Daven Nair <nair_260@hotma il.com> wrote:[color=blue]
      >Hi,
      >
      >I would like to know if Python supports codes similar to shell scripts:
      >
      >count=`ps -ef|grep "pattern"|w c -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`")[/color]

      Comment

      • William Park

        #4
        Re: Python code to replace shell scripts

        Daven Nair <nair_260@hotma il.com> wrote:[color=blue]
        > Hi,
        >
        > I would like to know if Python supports codes similar to shell scripts:
        >
        > count=`ps -ef|grep "pattern"|w c -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`")[/color]

        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, <opengeometry@y ahoo.ca>
        Linux solution for data management and processing.

        Comment

        • Dennis Lee Bieber

          #5
          Re: Python code to replace shell scripts

          Daven Nair fed this fish to the penguins on Thursday 11 December 2003
          10:30 am:
          [color=blue]
          > I would like to know if Python supports codes similar to shell
          > scripts:
          >
          > count=`ps -ef|grep "pattern"|w c -l`
          > for count in `echo $count`[/color]

          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").

          --[color=blue]
          > =============== =============== =============== =============== == <
          > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
          > wulfraed@dm.net | Bestiaria Support Staff <
          > =============== =============== =============== =============== == <
          > Bestiaria Home Page: http://www.beastie.dm.net/ <
          > Home Page: http://www.dm.net/~wulfraed/ <[/color]

          Comment

          • Alan Gauld

            #6
            Re: Python code to replace shell scripts

            On 12 Dec 2003 01:58:23 GMT, William Park <opengeometry@y ahoo.ca>
            wrote:
            [color=blue][color=green]
            > > count=`ps -ef|grep "pattern"|w c -l`
            > > for count in `echo $count`
            > > do
            > > done
            > > fi[/color][/color]

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

            print len(os.popen('p s -ef|grep "pattern"').rea d().split())
            [color=blue]
            > In any case, although Python does something well, shell does most things
            > better. (It's okey... I've got my helmet on.)[/color]

            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

            Comment

            • Cameron Laird

              #7
              Re: Python code to replace shell scripts

              In article <va5oa1-c93.ln1@beastie .ix.netcom.com> ,
              Dennis Lee Bieber <wlfraed@ix.net com.com> wrote:

              Comment

              • William Park

                #8
                Re: Python code to replace shell scripts

                Alan Gauld <alan.gauld@bti nternet.com> wrote:[color=blue]
                > On 12 Dec 2003 01:58:23 GMT, William Park <opengeometry@y ahoo.ca>
                > wrote:[color=green]
                > > In any case, although Python does something well, shell does most
                > > things better. (It's okey... I've got my helmet on.)[/color]
                >
                > 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.[/color]

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

                --
                William Park, Open Geometry Consulting, <opengeometry@y ahoo.ca>
                Linux solution for data management and processing.

                Comment

                • Serge Orlov

                  #9
                  Re: Python code to replace shell scripts


                  "William Park" <opengeometry@y ahoo.ca> wrote in message news:brd7nv$264 kc$2@ID-99293.news.uni-berlin.de...[color=blue]
                  > Alan Gauld <alan.gauld@bti nternet.com> wrote:[color=green]
                  > > On 12 Dec 2003 01:58:23 GMT, William Park <opengeometry@y ahoo.ca>
                  > > wrote:[color=darkred]
                  > > > In any case, although Python does something well, shell does most
                  > > > things better. (It's okey... I've got my helmet on.)[/color]
                  > >
                  > > 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.[/color]
                  >
                  > That is true, until you learn and program in shell.[/color]

                  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?)


                  Comment

                  • Alan Gauld

                    #10
                    Re: Python code to replace shell scripts

                    On 12 Dec 2003 20:17:04 GMT, William Park <opengeometry@y ahoo.ca>
                    wrote:[color=blue][color=green]
                    > > 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.[/color]
                    >
                    > That is true, until you learn and program in shell.[/color]

                    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

                    Comment

                    • Patrick TJ McPhee

                      #11
                      Re: Python code to replace shell scripts

                      [note follow-ups set to comp.lang.rexx]

                      In article <vtjp3e2bcihka4 @corp.supernews .com>,
                      Cameron Laird <claird@phaseit .net> 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
                      ptjm@interlog.c om

                      Comment

                      Working...