LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-26-2017, 11:47 AM   #1
peng12
LQ Newbie
 
Registered: Sep 2012
Posts: 15

Rep: Reputation: Disabled
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
 
Old 02-26-2017, 11:59 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,555

Rep: Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088
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 '\'' .

Last edited by rknichols; 02-26-2017 at 12:00 PM.
 
1 members found this post helpful.
Old 02-26-2017, 12:29 PM   #3
peng12
LQ Newbie
 
Registered: Sep 2012
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply

Tags
command line, expect


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] escaping single quote with regex ted_chou12 Linux - Newbie 2 06-26-2012 11:23 PM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM
[SOLVED] escaping a single quote from a bash script atbrew Programming 8 07-21-2010 09:21 AM
echo $PATH = /home/g3rc4n/bin:/usr/local/bin:/usr/bin:/bin:/usr/games ? i_heart_pandas Linux - Software 7 09-18-2009 08:33 AM
Double and single quote, Question in expect evilchild Programming 4 08-22-2004 05:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:38 AM.

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