简体   繁体   English

带引号的 rsync 的期望脚本

[英]Expect script for rsync with quotation marks

I want to rsync a folder to another machine.我想将文件夹同步到另一台机器。 sshpass is not an option and certificates don't either. sshpass 不是一个选项,证书也不是。 So I'm stuck with expect.所以我坚持期望。

So far, I have this bash script (minimized).到目前为止,我有这个 bash 脚本(最小化)。

read -p "Enter remote IP: " IP
read -p "Enter remote user: " USER
read -s -p "Enter remote password: " PASSWORD
expect <<EOF
    set timeout -1
    spawn rsync -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" \
        -aHv --exclude=".*/" --delete ../.. ${USER}@${IP}:~/destination
    expect "password: "
    send "${PASSWORD}\n"
    expect eof
EOF

Problem is that rsync doesn't respect the "--exclude" option.问题是 rsync 不尊重“--exclude”选项。 In the stdout, I see:在标准输出中,我看到:

spawn rsync -e ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -aHv --exclude=".*/" --delete ../.. pi@10.0.1.2:~/OberonHAP

Somehow, the quotes around the -e option vanished, but exclude still has them.不知何故, -e 选项周围的引号消失了,但 exclude 仍然有它们。

So I escape the quotes所以我逃避了引号

expect <<EOF
    set timeout -1
    spawn rsync -e \"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" \
        -aHv --exclude=".*/" --delete ../.. ${USER}@${IP}:~/destination
    expect "password: "
    send "${PASSWORD}\n"
    expect eof
EOF

but now I get但现在我明白了

Missing trailing-" in remote-shell command.

Note that the command itself works perfectly when typed directly into a Terminal.请注意,当直接输入终端时,命令本身可以完美运行。

I've also tried different things, like 'EOF' heredoc and passing in the variables via environment, but always get error messages of different sorts :-)我也尝试过不同的东西,比如'EOF' heredoc 和通过环境传递变量,但总是得到不同种类的错误消息:-)

How do I have to escape the rsync command so that it works fine?我必须如何转义 rsync 命令才能正常工作?

Tcl's syntax (and hence Expect's syntax) is not the same as that of the shell. Tcl 的语法(以及Expect 的语法)与shell 的不同。 In particular, the quoting rules are different;尤其是引用规则不同; you don't need to put double-quoting in the middle of a word as Tcl doesn't expand glob characters for you (you'd need to call glob explicitly if you wanted that).您不需要在单词中间放置双引号,因为 Tcl 不会为您扩展 glob 字符(如果需要,您需要显式调用glob )。 This means that your spawn command is actually best written as:这意味着你的spawn命令实际上最好写成:

spawn rsync -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" \
    -aHv --exclude=.*/ --delete ../.. ${USER}@${IP}:~/destination
#                ☝☝☝☝☝☝ LOOK HERE!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM