Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-28-2012, 03:17 AM   #1
LQ Newbie
Registered: Nov 2012
Posts: 5

Rep: Reputation: Disabled
Unhappy Sending Double quotas to expect script in bash

Hi everyone,

I'm trying to figure this out for a while now, with no luck. I have the following bash script block:

EXEC2=$(for i in "${rules[@]}"
expect -c "
spawn /usr/bin/ssh $username@$hostip
expect {

                     send \"yes\r\"

                     send \"$password\r\"
                     send \"\r\"
             \"o o o o\"
                     send \"internal command $i st 2\r\"
                     send \"f\"
		     send \"logout\r\"
		     send \"y\r\"
in the "internal command $i.." part, the $i is actually a variable that contains a line such as "this is a line" (quotas included).
When running this script, I get the following error:
extra characters after close-quote
    while executing 
"send "internal command "this is a line" -st 2\r"
			send "f"
			send "logout\r"
			send "y\r"
    invoked from within
"expect {

                        send "yes\r"

Now, I'm pretty sure that this problem related to the double Quotes that $i contains because when deleting it - everything work fine. The problem is that in my case "this is a line" contains white-spaces and so - I have to rap it with double-quotes (in the internal command)...
I tried every possible escape char with no luck so far...

Ideas, anyone??


Last edited by malony101; 11-29-2012 at 08:36 AM. Reason: code block
Old 11-28-2012, 12:50 PM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

I'm not at all familiar with expect, but one of the most common mistakes in the shell is attempting to store quotes or other shell syntax in variables.

I'm trying to put a command in a variable, but the complex cases always fail!

It may or may not be related to your problem, but if it's possible, try removing the quotes from the variable and instead include them in the expect script itself (assuming I'm reading the code correctly).

In addition, I believe the key line in your error message is this:
extra characters after close-quote
So your ultimate goal should be to locate these "extra characters", figuring out why they are appearing, and eliminate them. I'm guessing that the embedded quotes are forcing the parsing to somehow end prematurely, or similar.

Finally, I also highly suggest moving all that code into a function, and running that, instead of trying to shove it all into the $(..) bracket.
Old 12-03-2012, 02:38 AM   #3
LQ Newbie
Registered: Nov 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
First of all, Thank for replaying, David.
I did some additional testing and it seems there is some kind of a conflict when embedding expect script into bash.
When creating a "pure" expect script (starting with #!/bin/expect) - quotas are been sent without any problem.
when sending the same syntax as a command to expect under bash runtime - something goes wrong. It seems like although Expect should run the command - bash still reads the quotas and then throws the above error. I think I'm going to run in outside bash using an additional expect script. :-/

Last edited by malony101; 12-03-2012 at 02:39 AM.


bash scripting, expect, quotes

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
Problem running an Expect script within a Bash script mbeipi Programming 9 02-10-2018 05:00 AM
[SOLVED] Problem in exporting variable from bash script to expect script uk.engr Linux - Newbie 3 06-14-2012 01:57 AM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM
Assigning quotas in bash script jax8 Programming 3 04-23-2009 06:13 AM
Bash script for mail quotas Golgo13 Linux - Server 5 11-02-2008 04:07 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 PM.

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