LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-28-2012, 03:17 AM   #1
malony101
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:

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

             \"no)?\"
             {
                     send \"yes\r\"
                     exp_continue
             }

             \"password:\"
             {
                     send \"$password\r\"
                     send \"\r\"
                     exp_continue
             }
             \"o o o o\"
             {
                     send \"internal command $i st 2\r\"
                     send \"f\"
		     send \"logout\r\"
		     send \"y\r\"
             }
        }
interact
"
done)
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:
Code:
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 {

                "no)?"
                {
                        send "yes\r"
                        exp_continue
                }

      ..."
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??

Thanks.

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: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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!
http://mywiki.wooledge.org/BashFAQ/050

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:
Code:
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
malony101
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.
 
  


Reply

Tags
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 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