LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Sending Double quotas to expect script in bash (http://www.linuxquestions.org/questions/programming-9/sending-double-quotas-to-expect-script-in-bash-4175439081/)

malony101 11-28-2012 03:17 AM

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.

David the H. 11-28-2012 12:50 PM

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.

malony101 12-03-2012 02:38 AM

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. :-/


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