Using wait in a script

232 Views Asked by At

In the following expect script, I noticed that the script will send the password even before prompted for the password. How can this be prevented? Could I use a wait statement or something?

#!/usr/bin/expect -f
#set timeout 25
spawn rsync [email protected]:'/usr/backups /usr/backup-scripts /root/test/' /root/
expect "[email protected]'s password: $"
send "\$xxxxxx\n"
expect "\\$ $
1

There are 1 best solutions below

1
On

You shouldn't even need to use expect for this task. According to the man page, rsync supports an option to read a password directly from a file:

rsync --password-file=file_that_password_is_in [email protected]:'/usr/backups /usr/backup-scripts /root/test/' /root/

Or using an environment variable:

export RSYNC_PASSWORD=secret
rsync [email protected]:'/usr/backups /usr/backup-scripts /root/test/' /root/