execv putting quotes around arguments

M

Mike Vieths

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.
>>> import os
>>> arg0='/bin/ls'
>>> arg1='-l foo'
>>> args=[arg0]+[arg1]
>>> os.execv(arg0, args)
/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
 
D

Donn Cave

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

os.system(cmdline) 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, (e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top