LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem escaping single quote when using /usr/bin/expect -c (https://www.linuxquestions.org/questions/linux-newbie-8/problem-escaping-single-quote-when-using-usr-bin-expect-c-4175600628/)

peng12 02-26-2017 11:47 AM

Problem escaping single quote when using /usr/bin/expect -c
 
Hi all,

I'm using CHEF to automate the environment of my VPS.

My wish is now to create a MySQL user.
To prevent passing the MySQL-password as a command line arg I'm using /usr/bin/expect.
This seems to be more complicated than I thought.

Without CHEF and expect I would to:

Code:

mysql -uroot -p
[password]
MariaDB [(none)]> CREATE USER `test123`@`localhost` IDENTIFIED BY 'some_pass';

My CHEF recipe looks like this (I've removed the CHEF variables and substituted the username and password):
Code:

bash "create-mysql-user" do
    user "root"
    code <<-EOF
        /usr/bin/expect -c 'spawn  mysql -uroot -p
        expect "Enter password: "
        send "root_password"
        expect "MariaDB*none*>*"
        send "CREATE USER `test123`@`localhost` IDENTIFIED BY 'some_pass';\r"
        expect EOF'
        EOF
end

This produces: ERROR 1064 (42000): You have an error in your SQL syntax;....

Going through the log I see, that the single quotes around the passwords get removed.

Now: Whatever I tried, I couldn't get the single quotes in the final output.
I tried:
  • ... to escape them with \'
  • ... to writhe the single quotes twice: ''some_password''

So my question is: How can I use /usr/bin/expect with "-c" AND single quotes?

Thanks in advance and best regards

rknichols 02-26-2017 11:59 AM

Passing a literal single quote inside a single-quoted string is a bit tricky. Since the backslash character is not special inside that string, what you have to do is end the string with a single quote, then put in the escaped single quote, then continue the single-quoted string.
Code:

echo 'You can'\''t do this easily'
That's the string 'You can' followed by an escaped single quote and then the string 't do this easily'.

What it boils down to is representing each literal single quote with the 4-character sequence '\'' .

peng12 02-26-2017 12:29 PM

Thank you rknichols

that solved my question.

Code:

/usr/bin/expect -c 'send "This is a '\''test:'\''"'
results in
Code:

This is a 'test'
However: Chef still isn't passing the single quotes to my script. I can't exclude that this isn't a bug.
Therefore I've posted now on the chef-mailing-list.

Best regards


All times are GMT -5. The time now is 01:51 AM.