Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Run Unix shell command $ parse command line arguments in python

Reply
Thread Tools

Run Unix shell command $ parse command line arguments in python

 
 
rkoida@yahoo.com
Guest
Posts: n/a
 
      04-22-2005
Hello evryone

I am a newbie to python. I have a makefile which i can compile in
UNIX/LINUX, But i
I am planning to write a python script which actually does what my
MAKEFILE does. The make file is

#Makefile and some scripts to give output
#numbers
#Change till sign #END
var1:=564-574
a1 = 23
b1 = 678
kxm = ixm_7.gh
out = c$(var1)_s.gh
imageim
#END
#other files
file1 = sec$(b1).jk
file2 = sgf$(a1)
file3 = p$(b1).gh
prg3scriptfile = prg3_produce.xx
findoutdir = Dir:~/home/
x=67
#work
evrything: prg1 script2 prg3 script4
withoutprg1: script2 prg3 script4
prg1:
s$(ca)$(d) .

script2: Makefile
./script2 -i '$(file2)' -o '$(file1)' -a $(x) -n

prg3:
: > $(prg3scriptfile)
>> $(prg3scriptfile)

prg3 $(prg3scriptfile)
rm $(prg3scriptfile)
script4:
./script4 $(kxm) $(file2) $(out) $(var1)

I want to write a python script to replace this Makefile.
I Have tried using getopt/optparse for parsing command line options
How to Run Unix shell command from python.

My Task is

Myprg.py a1 b1 kxm out

Kindly suggest me some ideas/comments.

Thanks
koid wayne

 
Reply With Quote
 
 
 
 
Simon Brunning
Guest
Posts: n/a
 
      04-22-2005
On 21 Apr 2005 23:39:03 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> Kindly suggest me some ideas/comments.


Why don't you make a start, and come back to us with whatever problems
you encounter? We're happy to help, but we aren't going to do it for
you.

Andrew Dalke's "Wrapping command-line programs" series might come in
handy: http://www.dalkescientific.com/writi..._programs.html

--
Cheers,
Simon B,
(E-Mail Removed),
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
 
 
 
hue
Guest
Posts: n/a
 
      04-22-2005
Thanks for your reply

I started writing the script.. I have gone through documentation for
getopt

import string, getopt, sys

def usage():

print '''myscript.py -- uses getopt to recognize options
Options: -n -- No
-t -- T
-h -- help
-i -- i
-o -- Output:filename'''
sys.exit(1)
def main():

print "SYS ARGV: ", ",".join(sys.argv)

# Define the Options

Options = {
'n:': 'Number=',
't:': 'T',
'h' : 'help',
'i' : 'i',
'o' : 'Output_file',
}
shortOpts = ''.join(Options.keys())
longOpts = Options.values()


try:
(opts, args) = getopt.getopt(argv[1:], shortOpts, longOpts)
except getopt.error, msg:
print "Unrecognized argument or option"
# end try


for (opt, arg) in opts:

if opt in ('-n', '--Number'):
print '-n is the Number', Number
sys.exit()

elif opt in ('-t', '--T'):
print '-t is the T', T
sys.exit()

elif opt in ('-h', '--help'):
usage()
print " "
sys.exit()

elif opt in ('-i', '--i'):
print " I", i


elif opt in ('-o', '--Output Filename'):
print "Output", Output


# end if
# end for

print "OPTS: ", ",".join([repr(o) for o in opts])
print "ARGS: ", ",".join(args)

if __name__ == "__main__":
main()

with the above code, I am planning to do command line parsing. But how
to run unix shell command? DO i have to use os Module/ import command?

How should i proceed further, to
import commands
commands.getstatusoutput('ls /bin/ls')

Please suggest me some ideas how to proceed further

Thanks

 
Reply With Quote
 
Simon Brunning
Guest
Posts: n/a
 
      04-22-2005
On 22 Apr 2005 03:16:02 -0700, hue <(E-Mail Removed)> wrote:
> Thanks for your reply
>
> I started writing the script.. I have gone through documentation for
> getopt


(snip)

Good start. I tend to prefer optparse over getopt these days, but if
you've got it working the way you want it, you should probably stick
with what you have.

> with the above code, I am planning to do command line parsing. But how
> to run unix shell command? DO i have to use os Module/ import command?
>
> How should i proceed further, to
> import commands
> commands.getstatusoutput('ls /bin/ls')


You'll certainly need to import *something*. Have you looked at the
subprocess module?

--
Cheers,
Simon B,
(E-Mail Removed),
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
Terry Hancock
Guest
Posts: n/a
 
      04-23-2005
On Friday 22 April 2005 01:39 am, (E-Mail Removed) wrote:
> I am a newbie to python. I have a makefile which i can compile in
> UNIX/LINUX, But i
> I am planning to write a python script which actually does what my
> MAKEFILE does. The make file is

[...]
> I want to write a python script to replace this Makefile.
> I Have tried using getopt/optparse for parsing command line options
> How to Run Unix shell command from python.


Read the documentation on the "os" and "popen2" modules under
"generic operating system services" in the Python Standard Library
Manual (go to http://www.python.org if you haven't already got this
manual on hand).

Cheers,
Terry

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Jython code to parse command line arguments potchi Python 0 05-18-2011 05:58 AM
Can you run a command line script with arguments, without typing'ruby' first? Jayson Williams Ruby 28 11-30-2008 05:37 AM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM
can I run unix shell command in the ModelSim shell? clinton__bill@hotmail.com VHDL 2 02-18-2005 10:04 PM
Capture external command output line by line on Windows 98, shell problem. Dib Urim Perl Misc 4 12-03-2003 07:57 PM



Advertisments