5
expect -c 'spawn scp -C -o CompressionLevel=9 ~/partFiles/* abc@10.200.4.15:/export/home/abc/; sleep 10; expect password; send "secretPassword\n";interact'

throws - ~/partFiles/*: No such file or directory

Just

scp -C -o CompressionLevel=9 ~/partFiles/* abc@10.200.4.15:/export/home/abc/

however, works perfectly.

Why? How to fix this?

4

I think the ~ and * are expanded by the shell, but I bet expect invokes scp directly, bypassing the shell so those don't get expanded. You could try spawning sh -c the scp command.

If it's an option, it might also just be easier to share your key with the server though so you don't need expect for this at all.

Using the sh technique the command will end up looking like:

expect -c 'spawn sh -c "scp -C -o CompressionLevel=9 ~/partFiles/* abc@10.200.4.15:/export/home/abc/"; sleep 10; expect password; send "secretPassword\n";interact'
  • I didn't get it. What did you mean by You could try spawning sh -c the scp command. I am a little new to all this. Would you kindly explain? – Chani May 11 '15 at 14:38
  • Sure, I was just thinking that instead of using expect to spawn scp directly, which skips the filesystem expansion you want, you could spawn a shell to run the scp command in like: expect -c 'spawn sh -c "scp -C -o ..."' so you have a shell that will invoke your scp command and expand ~ and * for you – Eric Renouf May 11 '15 at 14:39
  • But thanks for the answer. – Chani May 11 '15 at 14:45
  • You have quotes around the password, which close the quotes for the sh -c You'll need to escape those to use this solution – Eric Renouf May 11 '15 at 14:51
  • 1
    Actually, I think the problem now is that the OP needs " after /export/home/rgh/; i.e., at the end of the scp command. – G-Man May 11 '15 at 14:54
4

An alternative to Eric's good answer: Tcl can do glob expansion

expect <<'END_EXPECT'
    set timeout -1      # use this instead of sleep
    set files [glob -nocomplain ~/partFiles/*]
    if {[llength $files]} {
        spawn scp -C -o CompressionLevel=9 {*}$files abc@10.200.4.15:/export/home/abc/
        expect password
        send "secretPassword\r"
        expect eof
    }
END_EXPECT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.