1

I meet problem with executing expect command with bash variable. I have location given by parameter of the script and I need to use it in send command via expect -c. But I need to give it with " so actually send command is thinking that I've ended typing and is recognizing rest of command as extra characters after close-quote. Thing is like:

#!/bin/bash

SIGN=ppp

expect -cd 'spawn ssh user@host
            expect "Password:"
            send -- "pass\r"
            expect "*~>"
            send -- "find /local/"$SIGN"/ -maxdepth 1 -type d -mtime +30 |xargs rm -rfv\r"
            expect "*~>"
            send -- "exit\r"'

Any idea how to send the variable from bash to expect without double-quotes? BTW - I cannot use keys for ssh

4 Answers 4

1

You can escape like that '"$SIGN"':

ex:

#!/bin/bash

SIGN=ppp

expect  -c 'spawn ssh tiago@localhost
            expect "password:"
            send -- "mypass\r"
            expect "~"
            send -- "echo '"$SIGN"'\r"
            expect "^ppp"
            send -- "exit\r"'
Sign up to request clarification or add additional context in comments.

Comments

0

What happens if you simply remove the "s around $SIGN?

send -- "find /local/$SIGN/ -maxdepth 1 -type d -mtime +30 |xargs rm -rfv\r"

alternatively try using {} around it.

send -- "find /local/${SIGN}/ -maxdepth 1 -type d -mtime +30 |xargs rm -rfv\r"

4 Comments

Without it, it's not considering it as variable: can't read "SIGN": no such variable. the same with {}
Weird. I think it's because it's inside the '' from the first line. Can you send them as a number of expect commands. One per line and skip the ''s? I'm sure there is a better way but that's where I would start
I actually don't get it. A number of expect commands? Won't it break ssh session? Could You give me a hint? :) I'm actually not familiar with expect at all
I'm not familiar with expect either. I was just looking at the bash syntax. I think the issue is the use of single quotes to contain the whole string. Take a look at this link stackoverflow.com/a/9420853/3757019. Not sure what the answer is but I think you need to use " instead if '
0

You could put that variable in the environment:

export SIGN=ppp

expect -cd '
    spawn ssh user@host
    expect "Password:"
    send -- "pass\r"
    expect "*~>"
    send -- "find /local/$env(SIGN)/ -maxdepth 1 -type d -mtime +30 |xargs rm -rfv\r"
    # ...................^^^^^^^^^^
    expect "*~>"
    send -- "exit\r"
'

Comments

0

I've faced one more problem: send -- "find /local/'"$SIGN"'/ -maxdepth 1 -type d -mtime +30 |grep -v '"$FILTER"' |xargs rm -rfv\r" This command is not executing. At least it's putting variables values correctly. But there is probably problem sending list of the folders to rm by xargs?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.