为什么这个期望代码不起作用?

Why does this expect code not work?

不确定这更像是一个 scripting/unix 问题还是一个编程问题,但我在 unix stackexchange 上试过但没有得到任何回应,所以:

以下 expect 代码似乎有效;也就是说,它似乎输入文本以回答密码提示。但是,该设备从未真正安装。

但是,如果我只是将命令输入 shell 并手动输入密码,设备就会成功安装。

所以我很好奇输入实际上在哪里结束,因为它似乎从未 'catch' 密码但也没有显示错误消息?事实上,两种情况下的输出看起来完全相同,但只有在 运行 命令和手动输入密码的情况下,我才能看到我的文件出现在网络上。

代码如下:

#!/usr/bin/expect
spawn sudo mount.cifs "//WinPC/My Pictures" /home/LinPC/Desktop/Pictures -o user=Me
expect "Password: " {
    set send_slow {1 .1}
    send -s "a_password"
}

更新:获得了我需要的帮助以使其正常工作:必须在发送密码后插入 'expect eof',以免密码过早结束。但是我现在希望将其更改为 'expect_background' 以便我可以对发出多个安装命令有相同的触发响应。下面提前结束,最后的'expect eof'导致错误:

expect_background "Password:" {
    send "a_password\r"
    expect eof
}
spawn sudo mount.cifs "//WinPC/My Pictures" /home/LinPC/Desktop/Pictures -o user=Me
expect eof

它应该是什么样子?

更新:以下代码块说明了当前的问题:

expect_background "Password: " {
    send "a_password\r"
    expect eof
}
spawn sudo mount.cifs "//WinPC/My Pictures" /home/LinPC/Desktop/Pictures -o user=someone

expect "Password: " {
    send "a_password\r"
    expect eof
}
#The password prompt gets answered by 'expect' but not 'expect_background'.
#If I delete the last 'expect' and insert 'expect eof' it hangs for a short
#while at the password prompt (around 3 seconds) then exits.
#Why?

发送密码后再添加一条expect语句。

send -s "a_password\r"
expect eof 

eof会让Expect等到程序结束。