【问题标题】:Expect script if command won't run如果命令不会运行,则期待脚本
【发布时间】:2015-03-18 01:56:12
【问题描述】:

谁能帮我解决以下问题。我认为这可能是由于空白,但对于我的生活,我无法弄清楚。

这应该是一个简单的脚本,它将根据文件的位置移动文件。我正在使用 expect,因为我希望它在我的 NAS 上处理(通过 ssh),而不是关闭文件并重新打开,只是为了在共享之间移动它。

#!/usr/bin/expect -f
# Script to organise downloaded file

set FileDir [lindex $argv 0]
set FileName [lindex $argv 1]
set MiscDir "/media/Misc/Downloads"
set DownDir "/media/Downloads"

if { [string compare $FileDIR  $DownDir]  = 0 } {

} elseif { [string compare $FileDIR  $MiscDir]  = 0 } {
        spawn ssh *****@*******
        expect "assword:"
        send "********\r"
        expect "$ "
        send "mv ~/Misc/Downloads/$FileNAME '~/Misc/To Convert/$FileNAME'"
        expect "$ "
        send "exit\r"
        expect eof'
}

更新代码:

#!/usr/bin/expect -f
# Script to organise downloaded file

set FileDir [lindex $argv 0]
set FileName [lindex $argv 1]
set MiscDir "/media/Misc/Downloads"
set DownDir "/media/Downloads"

if { [string compare $FileDir  $DownDir]  == 0 } {

} elseif { [string compare $FileDir  $MiscDir]  == 0 } {
        set OrigFile "\"/shares/Misc/Downloads/$FileName\""
        set MoveFile "\"/shares/Misc/To Convert/$FileName\""
        spawn ssh Admin@Appledore
        expect "assword:"
        send "xxxxxxxx\r"
        expect "$ "
        send "mv $OrigFile $MoveFile"
        expect "$ "
        send "exit\r"
        expect eof'
}

调试输出

spawn ssh Admin@Appledore parent: waiting for sync byte parent: telling child to go ahead parent: now unsynchronized from child spawn: returns {5222}

expect: does "" (spawn_id exp6) match glob pattern "assword:"? no Admin@appledore's password:  expect: does "Admin@appledore's password: " (spawn_id exp6) match glob pattern "assword:"? yes expect: set expect_out(0,string) "assword:" expect: set expect_out(spawn_id) "exp6" expect: set expect_out(buffer) "Admin@appledore's password:" send: sending "*******!\r" to { exp6 }

expect: does " " (spawn_id exp6) match glob pattern "$ "? no


expect: does " \r\n" (spawn_id exp6) match glob pattern "$ "? no [Admin@Appledore ~]$  expect: does " \r\n[Admin@Appledore ~]$ " (spawn_id exp6) match glob pattern "$ "? yes expect: set expect_out(0,string) "$ " expect: set expect_out(spawn_id) "exp6" expect: set expect_out(buffer) " \r\n[Admin@Appledore ~]$ " send: sending "mv "/shares/Misc/Downloads/NOOBS_lite_v1_4.zip" "/shares/Misc/To Convert/NOOBS_lite_v1_4.zip"" to { exp6 }

expect: does "" (spawn_id exp6) match glob pattern "$ "? no <ownloads/NOOBS_lite_v1_4.zip" "/shares/Misc/To Convert/NOOBS_lite_v1_4.zip"                                      expect: does "mv "/shares/Misc/Downloads/NOOBS_lite_v1_4.zip" "/shares/Misc/To Convert/NOOBS_lite_v1_4.zip\r<ownloads/NOOBS_lite_v1_4.zip" "/shares/Misc/To Convert/NOOBS_lite_v1_4.zip"                          \u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008\u0008" (spawn_id exp6) match glob pattern "$ "? no

【问题讨论】:

  • if {$FileDir eq $DownDir}会更清楚
  • 还要注意变量名:$FileDir$FileDIR 不是同一个变量
  • 谢谢 glenn - 我已经完成了,我已经更新了很多代码,但是如下所述,它在文件名的末尾附加了 exit。无论如何围绕这个?
  • 执行该移动的时间是否超过 10 秒?你可能想在send之前set timeout -1
  • 我的测试文件是一个 20Mb 的文件,它不应该花费超过 10 秒的时间(但似乎是)但是我处理的一些文件的大小是 2-3Gb。我只是尝试使用set timeout -1

标签: tcl expect


【解决方案1】:

把你的 = 改成 ==

= 通常用于赋值(在 tcl 中不起作用。如下所述)

== 是相等性测试

【讨论】:

  • 非常感谢 - 下一个问题。
  • 实际上,Tcl 根本没有= 运算符(赋值是通过set 命令完成的)。见expr documentation
  • 我已经改变了大部分,但我不能让它等待完成 mv 命令。例如,我正在尝试使用 zip 文件,并且在移动文件时将“退出”附加到文件扩展名
猜你喜欢
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 2014-08-24
  • 2019-11-26
  • 1970-01-01
相关资源
最近更新 更多