execute shell script from python, needs sys.argv

Discussion in 'Python' started by Matt, Nov 4, 2010.

  1. Matt

    Matt Guest

    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
    Matt, Nov 4, 2010
    #1
    1. Advertising

  2. On Thu, Nov 4, 2010 at 11:37 AM, Matt <> wrote:
    > 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)
    >


    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)


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


    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
    >
    Benjamin Kaplan, Nov 4, 2010
    #2
    1. Advertising

  3. Matt

    Peter Otten Guest

    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
    Peter Otten, Nov 4, 2010
    #3
  4. In message <>, 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. :)
    Lawrence D'Oliveiro, Nov 7, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    883
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  2. spec
    Replies:
    7
    Views:
    74,961
    Cameron Laird
    Jul 18, 2006
  3. Replies:
    2
    Views:
    422
    Dennis Lee Bieber
    Aug 20, 2006
  4. gmax2006
    Replies:
    7
    Views:
    590
    John Machin
    Aug 31, 2006
  5. moongeegee

    execute a shell script in a shell script

    moongeegee, Dec 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    237
    Ben Morrow
    Dec 4, 2007
Loading...

Share This Page