Shell – Unix AIX passing variable and arguments to expect and spawn

aixexpectscpshell-script

I am writing a script to scp some files but need to pass file location through the user or another script as an argument

#!/usr/bin/expect

set FILEPATH1 $1

spawn sh "scp $FILEPATH1 $USER@$HOST:/destination/files"
        set pass "pass123"
        expect {
        password: {send "$pass\r"; exp_continue}
                  }

also tried this before the spawn and before the #!/usr/bin/expect

FILEPATH1=$1

Best Answer

This isn't doing what you want:

set FILEPATH1 $1

Tcl (and expect) don't use the shell-style $1, $2, ... way of accessing the command line arguments. You want to use the global $argv list:

set FILEPATH1 [lindex $argv 0]

Also, your spawn call is incorrect. sh will be looking for a file named "scp $FILEPATH1 $USER@$HOST:/destination/files". You don't need a shell to spawn scp:

spawn scp $FILEPATH1 $USER@$HOST:/destination/files

Now, where are USER and HOST coming from? Are they environment variables? If so:

spawn scp $FILEPATH1 $env(USER)@$env(HOST):/destination/files

And, you're not waiting for the transfer to actually complete before your script exits. Do this:

expect password:
send "$pass\r"
set timeout -1
expect eof

I'm assuming that you're only prompted for the password once. If you have to enter it multiple times, then

expect {
    password: {
        send "$pass\r"
        set timeout -1
        exp_continue
    }
    eof
}
Related Question