Hi All,
I am trying to execute a shell script from within python.. This shell
script takes the format, where $1 and $2 are variables from the
command line: cat $1 | Fastx_trimmer -n COUNT -o $2
straight into the cmd line it would be: cat file.1 | Fastx_trimmer -n
COUNT -o file.2
So, know that there is a way to do this in python using the
subprocess module, but despite a lot of effort, I can't seem to get
this to work, and precisely because of those arguments taken from the
command line.
I was thinking that the easiest thing to so was to
import sys, os, subprocess
proc = subprocess.call([cat sys.argv[1] | fastx_trimmer -n COUNT -o
sys.argv[2]], shell=True)
this clearly does not work...
alternatively, I could put the shell command in its own file, say
fastx.sh, and pass it's arguments to it vie the command line.
import sys, os, subprocess
proc = subprocess.call([fastx.sh, sys.argv[1], sys.argv[2]],
shell=True)
But, this does not seem to work as this is not the proper way to pass
arguments to the shell script.
in short, I'm sure that this is a easy fix, but given my still limited
python vocabulary, it eludes me.
Thanks, Matt
|
|
0
|
|
|
|
Reply
|
Matt
|
11/4/2010 3:37:27 PM |
|
On Thu, Nov 4, 2010 at 11:37 AM, Matt <macmanes@gmail.com> wrote:
> Hi All,
>
> I am trying to execute a shell script from within python.. =A0This shell
> script takes the format, where $1 and $2 are variables from the
> command line: cat $1 | Fastx_trimmer -n COUNT -o $2
>
> straight into the cmd line it would be: =A0cat file.1 | Fastx_trimmer -n
> COUNT -o file.2
>
> So, =A0know that there is a way to do this in python using the
> subprocess module, but despite a lot of effort, I can't seem to get
> this to work, and precisely because of those arguments taken from the
> command line.
>
> I was thinking that the easiest thing to so was to
>
> import sys, os, subprocess
> proc =3D subprocess.call([cat sys.argv[1] | fastx_trimmer -n COUNT -o
> sys.argv[2]], shell=3DTrue)
>
Python is not the shell. Shell commands are not python commands. You
need either a string or a list of strings, so any literal have to be
in quotes. Also, subprocess can't handle the redirection. You need to
run it as two commands.
proc1 =3D subprocess.Popen(["cat", sys.argv[1]],stdout =3D
subprocess.PIPE, shell =3D True)
proc2 =3D subprocess.Popen(["fastx_trimmer", "-n", "COUNT", "-o",
sys.argv[2]],stdin=3Dproc1.stdout, shell=3DTrue)
> this clearly does not work...
>
> alternatively, I could put the shell command in its own file, say
> fastx.sh, and pass it's arguments to it vie the command line.
>
> import sys, os, subprocess
> proc =3D subprocess.call([fastx.sh, sys.argv[1], sys.argv[2]],
> shell=3DTrue)
>
Again, you need a string. fastx.sh looks for a python object called
fastx and tries accessing an attribute called sh in that object. Ov
course, there's no such thing. Put quotes around it and it will work.
> But, this does not seem to work as this is not the proper way to pass
> arguments to the shell script.
>
> in short, I'm sure that this is a easy fix, but given my still limited
> python vocabulary, it eludes me.
>
> Thanks, Matt
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
|
|
0
|
|
|
|
Reply
|
Benjamin
|
11/4/2010 4:06:17 PM
|
|
Matt wrote:
> I am trying to execute a shell script from within python.. This shell
> script takes the format, where $1 and $2 are variables from the
> command line: cat $1 | Fastx_trimmer -n COUNT -o $2
>
> straight into the cmd line it would be: cat file.1 | Fastx_trimmer -n
> COUNT -o file.2
>
> So, know that there is a way to do this in python using the
> subprocess module, but despite a lot of effort, I can't seem to get
> this to work, and precisely because of those arguments taken from the
> command line.
>
> I was thinking that the easiest thing to so was to
>
> import sys, os, subprocess
> proc = subprocess.call([cat sys.argv[1] | fastx_trimmer -n COUNT -o
> sys.argv[2]], shell=True)
>
> this clearly does not work...
>
> alternatively, I could put the shell command in its own file, say
> fastx.sh, and pass it's arguments to it vie the command line.
>
> import sys, os, subprocess
> proc = subprocess.call([fastx.sh, sys.argv[1], sys.argv[2]],
> shell=True)
>
> But, this does not seem to work as this is not the proper way to pass
> arguments to the shell script.
>
> in short, I'm sure that this is a easy fix, but given my still limited
> python vocabulary, it eludes me.
You could do it in two steps:
>>> from subprocess import *
>>> source = Popen(["cat", "/usr/share/dict/words"], stdout=PIPE)
>>> call(["wc"], stdin=source.stdout)
98569 98568 931708
0
>>>
A similar example is here, under a "can't miss" headline:
http://docs.python.org/library/subprocess.html#replacing-shell-pipeline
Peter
|
|
0
|
|
|
|
Reply
|
Peter
|
11/4/2010 4:11:06 PM
|
|
Benjamin Kaplan <benjamin.kaplan@case.edu> wrote:
>
>Python is not the shell. Shell commands are not python commands. You
>need either a string or a list of strings, so any literal have to be
>in quotes. Also, subprocess can't handle the redirection. You need to
>run it as two commands.
>
>proc1 = subprocess.Popen(["cat", sys.argv[1]],stdout =
>subprocess.PIPE, shell = True)
>proc2 = subprocess.Popen(["fastx_trimmer", "-n", "COUNT", "-o",
>sys.argv[2]],stdin=proc1.stdout, shell=True)
I KNOW that we're still working on syntax here, and that it's too early for
optimization, but it bothers me to see "cat" as the first thing in a
pipeline. You don't actually need two steps here at all:
proc1 = subprocess.Popen(
["fastx_trimmer", "-n", "COUNT", "-o", sys.argv[2],
stdin=open(sys.argv[1]), shell=True
)
With this, I don't think you even need "shell=True".
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
|
|
0
|
|
|
|
Reply
|
Tim
|
11/6/2010 10:53:48 PM
|
|
In message <1vmbd65uaj2snq1v0vo49ktn0lsc2o5m1a@4ax.com>, Tim Roberts wrote:
> I KNOW that we're still working on syntax here, and that it's too early
> for optimization, but it bothers me to see "cat" as the first thing in a
> pipeline.
An anti-UUOC instinct. Very good. :)
|
|
0
|
|
|
|
Reply
|
Lawrence
|
11/7/2010 8:48:10 AM
|
|
|
4 Replies
318 Views
(page loaded in 0.43 seconds)
Similiar Articles: execute shell script from python, needs sys.argv - comp.lang ...Hi All, I am trying to execute a shell script from within python.. This shell script takes the format, where $1 and $2 are variables from the co... How can I get a Javascript command to run a Shell script? - comp ...execute shell script from python, needs sys.argv - comp.lang ... How can I get a Javascript command to run a Shell script? - comp ... execute shell script from python ... how to execute a .bat from Javascript? - comp.text.pdfexecute shell script from python, needs sys.argv - comp.lang ... How can I get a Javascript command to run a Shell script? - comp ... execute shell script from python ... Unable to execute shell script - comp.unix.solarisexecute shell script from python, needs sys.argv - comp.lang ... Unable to execute shell script - comp.unix.solaris execute shell script from python, needs sys.argv - comp ... Cant get "grant execute on procedure" to work. - comp.databases ...execute shell script from python, needs sys.argv - comp.lang ... Hi All, I am trying to execute a shell script ... module, but despite a lot of effort, I can't seem to get ... Pass command line parameters when running an application from a ...execute shell script from python, needs sys.argv - comp.lang ... Pass command line parameters when running an application from a ... execute shell script from python ... gawk/Windows/cmd.exe/ARGV - comp.lang.awkexecute shell script from python, needs sys.argv - comp.lang ... gawk/Windows/cmd.exe/ARGV - comp.lang.awk... File Open not executing.... - comp.graphics.apps.paint ... do ... format command not work - comp.unix.solarisexecute shell script from python, needs sys.argv - comp.lang ... This shell script takes the format, where $1 and $2 are ... sys.argv[2]], shell=True) this clearly does ... fork(): how can I kill forked process and all ITS children, but ...... problem I am having now is that if I call a shell scripts ... stdio.h> #include <sys/types.h> #include <sys ... child process */ /* printf("trying to execute: %s\n",argv ... Wrap a function - comp.lang.pythonBut a script full of run ("shell_command ... No need to re-invent the wheel. I > don't think Python will ever beat sh as a shell replacement. ... comp.soft-sys.matlab ... execute shell script from python, needs sys.argv - comp.lang ...Hi All, I am trying to execute a shell script from within python.. This shell script takes the format, where $1 and $2 are variables from the co... 7/21/2012 7:30:43 PM
|