Python code to replace shell scripts

D

Daven Nair

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
 
E

Edvard Majakari

Daven Nair said:
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";
 
C

Cameron Laird

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
....?
 
W

William Park

Daven Nair said:
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.)
 
D

Dennis Lee Bieber

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

--
 
A

Alan Gauld

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
 
C

Cameron Laird

.
.
.
(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.
 
W

William Park

Alan Gauld said:
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.
 
S

Serge Orlov

William Park said:
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?)
 
A

Alan Gauld

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
 
P

Patrick TJ McPhee

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

% 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top