fab-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Fab-user] Problems escaping double and single quotes within run()


From: Todd DeLuca
Subject: Re: [Fab-user] Problems escaping double and single quotes within run()
Date: Wed, 16 Jan 2013 17:42:55 -0500

Hi Tim,

Your problems might stem from a combination of how python interprets backslashes in strings and how bash handles them in single quoted strings.

Here is a page explaining how to put a quote into a single quoted string.  Don't use a backslash.


Here is an example of some wrong ways to do this and one right way, in fabric.

First, create a test file:

    echo "foo'bar" > ~/foo.txt

Then create a fabric task in your fabfile.py:

    from fabric.api import settings, run, task
    import os

    @task
    def test():
        with settings(warn_only=True):
            # wrong: String is not raw, so python interprets backslash.
            run("grep 'foo\'bar' " + os.path.expanduser('~/foo.txt'))
            # wrong: Bash doesn't expand backslashes in single quoted strings.
            run(r"grep 'foo\'bar' " + os.path.expanduser('~/foo.txt'))
            # right: Raw string in python and backslashed quote outside of single-quoted string in bash.
            run(r"grep 'foo'\''bar' " + os.path.expanduser('~/foo.txt'))


Then run the task:

    address@hidden:~/work/roundup$ fab --hosts=localhost test
    [localhost] Executing task 'test'
    [localhost] run: grep ''' /Users/td23/foo.txt
    [localhost] out: /bin/bash: -c: line 0: unexpected EOF while looking for matching `''
    [localhost] out: /bin/bash: -c: line 1: syntax error: unexpected end of file
    [localhost] out: 


    Warning: run() received nonzero return code 2 while executing 'grep ''' /Users/td23/foo.txt'!

    [localhost] run: grep ''''' /Users/td23/foo.txt
    [localhost] out: /bin/bash: -c: line 0: unexpected EOF while looking for matching `''
    [localhost] out: /bin/bash: -c: line 1: syntax error: unexpected end of file
    [localhost] out: 


    Warning: run() received nonzero return code 2 while executing 'grep ''''' /Users/td23/foo.txt'!

    [localhost] run: grep '\'' /Users/td23/foo.txt
    [localhost] out: /bin/bash: -c: line 0: unexpected EOF while looking for matching `''
    [localhost] out: /bin/bash: -c: line 1: syntax error: unexpected end of file
    [localhost] out: 


    Warning: run() received nonzero return code 2 while executing 'grep '\'' /Users/td23/foo.txt'!

    [localhost] run: grep ''\''' /Users/td23/foo.txt
    [localhost] out: 'hello'
    [localhost] out: 


    Done.
    Disconnecting from localhost... done.

I hope that helps to unravel some of your quote chaos.

Regards,
Todd


On Wed, Jan 16, 2013 at 3:29 PM, Tim O'Guin <address@hidden> wrote:
Hi, all.

First off, I'm new to Fabric and Python, so I apologize if this is
something obvious that I'm doing wrong.

I'm writing a program to audit an XML config file on remote hosts.
Specifically, I'm auditing the configuration file for Pidgin to ensure
that chat logging is disabled. I'm attempting to use grep to find the
relevant lines and sed to pull out the True/False bool.

Here are the two relevant lines of code:

imbool = sudo("grep '<pref name=\'log_ims\' type=\'bool\'
value=\'[0|1]\'/>' " + configfile + " | sed 's/.*
value=\'\([01]\)\'.*/\1/'")
chatbool = sudo("grep '<pref name=\'log_chats\' type=\'bool\'
value=\'[0|1]\'/>' " + configfile + " | sed 's/.*
value=\'\([01]\)\'.*/\1/'")

I've gone around and around trying to escape quotes in different ways,
but none of them are working. I was told in the IRC channel that
escaping quotes is problematic.

I figured I'd make sure I'm not just missing something obvious before
I figure out the Python way to do it (i.e., without grep and sed).

Thanks!

- Tim O'Guin

_______________________________________________
Fab-user mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/fab-user



--
Todd DeLuca
http://todddeluca.com
http://wall.hms.harvard.edu/


reply via email to

[Prev in Thread] Current Thread [Next in Thread]