Could anybody please tell me why this is not working?
#!/bin/bash
cd /home
touch somefile
/usr/bin/expect<<FILETRANSFER
spawn scp -r -P remoteServerPort somefile remoteServerIP:/home
expect "assword:"
send "MyPassWord\r"
interact
FILETRANSFER
echo "It's done"
It doesn't give any error but file is not transferred to remote server.I have tried many ways still couldn't find any solution.
Expect For Loop Examples: for {initialization} {conditions} {incrementation or decrementation} { ... } Expect for loop example : for {set i 1} {$i < $no} {incr i 1} { set $total [expr $total * $i ] } puts "$total"; Note: You should place the loop open brace in the same line as it contains “for” keyword.
Bash scripts provide many programs and features to carry out system automation tasks. The expect command offers a way to control interactive applications which require user input to continue.
Copy a File ( cp ) You can also copy a specific file to a new directory using the command cp followed by the name of the file you want to copy and the name of the directory to where you want to copy the file (e.g. cp filename directory-name ). For example, you can copy grades. txt from the home directory to documents .
The bash script you have defined is passing the expect commands on the standard input of expect
. However, the expect
command requires its arguments on a file or as an argument using the -c
option.
You have several options but to add the less modifications on your script you just need to use the process substitution to create a here-document (temporary) for the expect
command.
#!/bin/bash
echo "[DEBUG] INIT BASH"
cd /home
touch somefile
/usr/bin/expect <(cat << EOF
spawn scp -r -P remoteServerPort somefile remoteServerIP:/home
expect "Password:"
send "MyPassWord\r"
interact
EOF
)
echo "[DEBUG] END BASH"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With