Quotation Ugliness

Tim Chase python.list at tim.thechases.com
Wed Nov 26 07:56:06 EST 2014


On 2014-11-26 00:04, Tim Daneliuk wrote:
> someprog.py uname && sudo cat /etc/sudoers
> 
> vs.
> 
> someprog.py uname && echo "sudo cat /etc/suoders"
> 
> 
> In the first instance, I need the sudo passoword, in the second I
> don't.

This doesn't jibe with the pairs of quotes you sent and your request
for nesting.  In most popular shells, the majority of your "quote"
characters don't actually quote anything:

  bash$ echo // hello
  hello
  bash$ echo /* hello */
  [returns all the items in my root directory, the word "hello",
  along with all the sub-directories in the current directory]
  bash$ echo this#and#that
  this#and#that
  bash$ echo this # and #that
  this

and has problems with things like

  someprog.py uname && su""do cat /etc/sudoers
  someprog.py uname && s"ud"o cat /etc/sudoers

which my shell will parse valid execution of sudo.  

You state the problem as "I *just* want to find whether something is
quoted or not", but you have to clearly define what quoted means.
The standard library does provide the shlex module which comes fairly
close to what it sounds like you describe:

  def is_sudo(corpus):
    return "sudo" in shlex.split(corpus, comments="#")

but that still hiccups on

  >>> is_sudo("echo I love sudo")
  True

-tkc












More information about the Python-list mailing list