Using expect, spawn with scp

expectscpshellspawn

I'm currently trying to automate a file transfer using the scp command with a shell script and the expect package. Based on what I've seen it seems that I should have
#!/usr/bin/expect -f
But when I did that I still get the errors:

DirectoryChange.sh: line 33: spawn: command not found
couldn't read file "*Password:*": no such file or directory
DirectoryChange.sh: line 35: send: command not found
DirectoryChange.sh: line 36: interact: command not found

The code I have works something along these lines:

#!/usr/bin/expect -f

repository=$PWD"/subdirectory/"
set pass "***********"

cd $repository
spawn scp -r user@host:/copyDirectory/ .
expect "*Password:*"
send "${pass}\r";
interact

Best Solution

It's a bad practice to store passwords in scripts or any other file. Use SSH authentication keys instead.

Take a look at this tutorial.