execv putting quotes around arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Vieths

    execv putting quotes around arguments

    I'm running into a problem when I try to run commands with os.execv. It
    seems to be putting quotation marks around each element of the list
    passed as its second argument. This is fine for the most part, but if
    the argument has a space in it, getopt (in the command being called)
    will read the entire quoted string as the argument and generally fail.

    Here's an example:

    Python 2.2.2 (#1, Jan 30 2003, 21:26:22)
    [GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> import os
    >>> arg0='/bin/ls'
    >>> arg1='-l foo'
    >>> args=[arg0]+[arg1]
    >>> os.execv(arg0, args)[/color][/color][/color]
    /bin/ls: invalid option --
    Try `/bin/ls --help' for more information.

    This can be worked around by breaking '-l foo' into two seperate
    elements ('-l' and 'foo'), but that's not always intuitive. Anyone know
    why those quotes are there, and if there's a way to make them go away?
    I'm stuck with execv, since this is part of a larger project for which
    I'm creating a module, and modifying that portion to use os.popen,
    os.system, or something similar isn't an option.

    Mike Vieths

  • Donn Cave

    #2
    Re: execv putting quotes around arguments

    In article <4023c273$0$412 95$a1866201@new sreader.visi.co m>,
    Mike Vieths <foeclan@yahoo. com> wrote:[color=blue]
    > I'm running into a problem when I try to run commands with os.execv. It
    > seems to be putting quotation marks around each element of the list
    > passed as its second argument. This is fine for the most part, but if
    > the argument has a space in it, getopt (in the command being called)
    > will read the entire quoted string as the argument and generally fail.[/color]
    ....[color=blue]
    > This can be worked around by breaking '-l foo' into two seperate
    > elements ('-l' and 'foo'), but that's not always intuitive. Anyone know
    > why those quotes are there, and if there's a way to make them go away?
    > I'm stuck with execv, since this is part of a larger project for which
    > I'm creating a module, and modifying that portion to use os.popen,
    > os.system, or something similar isn't an option.[/color]

    os.system(cmdli ne) is about the same as os.fork()
    followed by os.execv('/bin/sh', ['sh', '-c', cmdline])

    The way execv works is generally a strong advantage,
    because it doesn't automatically run the shell over
    its parameters (it doesn't `add quotes', it just
    doesn't need them), and that shell step opens you
    up to all kinds of unexpected possibilities if you
    don't positively know what's going into the command
    line. But if you want the shell, you can have it -
    just invoke it.

    Donn Cave, donn@u.washingt on.edu

    Comment

    Working...