How to Handle Errors in expect When rsync Fails
When using expect to execute rsync, you may encounter errors such as “Missing trailing-’ in remote-shell command. rsync error: syntax or usage error (code 1) at main.c(430) [sender=3.1.1]”. This explains the cause and solution for this error.
Error Overview
This is an error that I had left unresolved for two years, but after researching, I found a solution. When this error occurs, you see a message like this:
Missing trailing-' in remote-shell command.
rsync error: syntax or usage error (code 1) at main.c(430) [sender=3.1.1]
Example Code That Causes the Error
Here’s an example of an expect script that produces this error:
#!/usr/bin/expect -f
# exp_internal 1 ;# uncomment to turn on expect debugging
set timeout -1
spawn rsync -av -e 'ssh -p 10022 -i ~/.ssh/yuis.key' hoge.md [email protected]:~/yuis.xsrv.jp/public_html/_tmp
expect "Enter passphrase"
send "yourownpassword\n"
expect eof
Cause
The cause of the error is the use of single quotes in the rsync command. When single quotes are used, rsync may not interpret the command correctly.
Reference links:
Solution
By changing the single quotes to double quotes, you can avoid the error. Here’s the corrected code:
#!/usr/bin/expect -f
# exp_internal 1 ;# uncomment to turn on expect debugging
set timeout -1
spawn rsync -av -e "ssh -p 10022 -i ~/.ssh/yuis.key" hoge.md [email protected]:~/yuis.xsrv.jp/public_html/_tmp
expect "Enter passphrase"
send "yourownpassword\n"
expect eof
With this change, the rsync command should execute normally.