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

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
jester2580
  • 113
  • 1
  • 7

4 Answers4

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"'
Tiago Lopo
  • 7,619
  • 1
  • 30
  • 51
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"

Robert Ekendahl
  • 277
  • 2
  • 11
  • Without it, it's not considering it as variable: can't read "SIGN": no such variable. the same with {} – jester2580 Jun 20 '14 at 12:46
  • 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 – Robert Ekendahl Jun 20 '14 at 12:50
  • 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 – jester2580 Jun 20 '14 at 12:56
  • 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 http://stackoverflow.com/a/9420853/3757019. Not sure what the answer is but I think you need to use `"` instead if `'` – Robert Ekendahl Jun 20 '14 at 13:00
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"
'
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
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?

jester2580
  • 113
  • 1
  • 7