LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-22-2008, 03:31 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Python: need to pass quoted string to an exec call; prob w/ escaped chars


I need to run this on the command line:

Code:
./prog -command 'trace("foo")'
... but it needs to be run by python, so I'm trying assemble the args & run like so:

Code:
  args = ['prog','-command \'trace("foo")\'']
  os.execvp("prog",args)
but I keep getting errors that seem to be related to the escaping of quotes and such. For example:

Code:
['prog', '-command \\\'trace("hello")\\\'']
Unmatched '.

  ----  or ----

['prog', '-command \'trace(\\"hello\\")\'']
Badly placed ()'s.
Not quite sure how to do this.

If I run from the command line, this works:

./prog -command 'trace("foo")'

so how can I get that to happen from within python?
 
Old 12-22-2008, 03:35 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
My suggestion would be to use double-quotes instead of single-quotes, as single-quotes do not allow escape characters.

Code:
['prog', "-command \'trace(\"hello\")\'"]
 
Old 12-22-2008, 05:30 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by indienick View Post
My suggestion would be to use double-quotes instead of single-quotes, as single-quotes do not allow escape characters.

Code:
['prog', "-command \'trace(\"hello\")\'"]
Rest assured, I've tried all the possible incarnations of quotes.
 
Old 12-22-2008, 07:35 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can try the subprocess module. see the docs for examples
 
Old 12-22-2008, 07:49 PM   #5
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by ghostdog74 View Post
you can try the subprocess module. see the docs for examples
Specifically, what does the subprocess module accomplish that execvp doesn't with regards to the formatting of arguments? I'm pretty familiar with the subprocess module, but I'm not seeing it as a path to a solution to this problem (but will be happily corrected if I'm wrong)
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: how do you replace unicode chars in large text files? BrianK Programming 1 12-19-2008 12:54 AM
How to pass a result of exec command in perl to a variable??? HyperTrey Programming 3 05-23-2008 12:47 PM
Bash: how do I pass a double quoted string to a command? Nylex Programming 8 04-18-2007 09:36 AM
How to call the modules and pass parameters to it? shendeswanand Linux - Software 1 12-31-2005 09:18 PM
Python EXEC Statement ranger_nemo Programming 3 07-24-2003 08:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration