0

I need to use a find command in a script with ssh and find together. Something like this:

expect -c "
    spawn ssh user@host \"find api -type f -name *.html -mtime +30 -exec rm -rf {} \;\"
    expect {
        \"*password\" {set timeout 300; send \"mypassword\r\";}
          }
    expect eof"

And I get

find: missing argument to `-exec'

Any thoughts on what may be causing this or how I can fix it ? Thx

Cynthia
  • 96
  • 7
  • 2
    Possible duplicate of [Use expect in bash script to provide password to SSH command](https://stackoverflow.com/questions/4780893/use-expect-in-bash-script-to-provide-password-to-ssh-command) – l0b0 Jun 12 '19 at 10:34
  • 1
    You should rewrite your script as suggested above, because your way you will need to replace `\;` by `\\\;` – meuh Jun 12 '19 at 11:54
  • @meuh, only need 2 backslashes. or use single quotes to protect the semicolon. – glenn jackman Jun 12 '19 at 13:31
  • @meuh You are right. It works . Thank you very much . – Cynthia Jun 13 '19 at 03:41
  • take a look at [sexpect (Expect for Shells)](https://github.com/clarkwang/sexpect) which you can use to write *Expect* scripts with **shell code only**. – pynexj Jun 13 '19 at 04:09

1 Answers1

0

I find heredocs to be helpful to avoid quoting hell

expect << 'END_EXPECT'
    spawn ssh user@host "find api -type f -name *.html -mtime +30 -exec rm -rf {} \;"
    expect "*password" {set timeout 300; send "mypassword\r"}
    expect eof
END_EXPECT
glenn jackman
  • 238,783
  • 38
  • 220
  • 352