473,382 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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"|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
Jul 18 '05 #1
10 2263
"Daven Nair" <na******@hotmail.com> 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!

$_ = '456476617264204d616a616b6172692c20612043687269737 469616e20'; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60281449,'es'),2,4),"\n";
Jul 18 '05 #2
In article <ma************************************@python.org >,
Daven Nair <na******@hotmail.com> 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`")

Jul 18 '05 #3
Daven Nair <na******@hotmail.com> 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, <op**********@yahoo.ca>
Linux solution for data management and processing.
Jul 18 '05 #4
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").

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #5
On 12 Dec 2003 01:58:23 GMT, William Park <op**********@yahoo.ca>
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
Jul 18 '05 #6
In article <va************@beastie.ix.netcom.com>,
Dennis Lee Bieber <wl*****@ix.netcom.com> wrote:
Jul 18 '05 #7
Alan Gauld <al********@btinternet.com> wrote:
On 12 Dec 2003 01:58:23 GMT, William Park <op**********@yahoo.ca>
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, <op**********@yahoo.ca>
Linux solution for data management and processing.
Jul 18 '05 #8

"William Park" <op**********@yahoo.ca> wrote in message news:br************@ID-99293.news.uni-berlin.de...
Alan Gauld <al********@btinternet.com> wrote:
On 12 Dec 2003 01:58:23 GMT, William Park <op**********@yahoo.ca>
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?)
Jul 18 '05 #9
On 12 Dec 2003 20:17:04 GMT, William Park <op**********@yahoo.ca>
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
Jul 18 '05 #10
[note follow-ups set to comp.lang.rexx]

In article <vt************@corp.supernews.com>,
Cameron Laird <cl****@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
pt**@interlog.com
Jul 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Erik Johnson | last post by:
This is somewhat a NEWBIE question... My company maintains a small RDBS driven website. We currently generate HTML using PHP. I've hacked a bit in Python, and generally think it is a rather...
4
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or...
42
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
20
by: xeys_00 | last post by:
I posted a article earlier pertaining programming for my boss. Now I am gonna ask a question about programming for myself. I just finished my first C++ Class. Next semester is a class on...
8
by: Jan Danielsson | last post by:
Hello all, How do I make a python script actually a _python_ in unix:ish environments? I know about adding: #!/bin/sh ..as the first row in a shell script, but when I installed python on...
53
by: Michael Tobis | last post by:
Someone asked me to write a brief essay regarding the value-add proposition for Python in the Fortran community. Slightly modified to remove a few climatology-related specifics, here it is. I...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 430 open ( -4) / 3447 closed (+17) / 3877 total (+13) Bugs : 922 open ( -7) / 6316 closed (+31) / 7238 total (+24) RFE : 245 open...
3
by: koutoo | last post by:
Is it possible to display messages in the python shell? I want to display error messages based on parameters in my scripts to the users. Is there another way to display messages other than log...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.