为什么这个期望脚本不匹配这个glob模式(包含调试)

时间:2015-12-26 03:02:27

标签: expect

我期望脚本的这一部分有时发送“y \ r”,有时不发送。另外,我甚至不知道为什么lvcreate会问,因为在此之前,脚本dd的驱动器有20GB的零,这是第一个lvcreate。但是,无论如何,这将是一个单独的问题,而我只是建立在擦除的响应中。 (并且,是的,这种类型的东西很危险。它用于新系统安装,并逐步让用户让他们输入几个确认。)

我不明白为什么它不匹配第一个glob。第一行是程序lvcreate的实际输出。

WARNING: linux_raid_member signature detected on /dev/disk1/terraswap1 at offset 4096. Wipe it? [y/n]: 
expect: does "\u001b[K\u001b[?1h\u001b=\u001b[?2004hl\u0008lvcreate disk1 -L 16G -n terraswap1\u001b[?1l\u001b>\u001b[?2004l\r\r\n\u001b]0;root@archiso: lvcreate disk1 -L 16G -n terraswap1\u0007WARNING: linux_raid_member signature detected on /dev/disk1/terraswap1 at offset 4096. Wipe it? [y/n]: " (spawn_id exp7) match glob pattern "Wipe it? [y/n]: "? no
"root*archiso*# "? no

简而言之,为减少滚动,期望输出为:

expect: does "...Wipe it? [y/n]: " (spawn_id exp7) match glob pattern "Wipe it? [y/n]: "? no
"root*archiso*# "? no

相关的代码部分是:

#!/usr/bin/expect -f

# ... connect via ssh as root to machine with hostname archiso

expect "root*archiso*# "
send "lvcreate disk1 -L 16G -n terraswap1\r"
expect {
   "Wipe it? \[y/n]: " {
      send "y\r"
      exp_continue
   } "root*archiso*# " {
      send "lvcreate disk2 -L 16G -n terraswap2\r"
   }
}

2 个答案:

答案 0 :(得分:0)

[对于TclExpect模式匹配器都是特殊的,所以它特别凌乱。要匹配文字[,您必须从Tcl反斜杠一次,然后再次反复使其在模式匹配期间不被视为范围。当然,第一个反斜杠必须被反斜杠以防止它将下一个反斜杠转换成字面反斜杠!

expect "\\\[" ; #matches literal '['

所以,你的代码应该是,

expect {
   "Wipe it? \\\[y/n]: " {
      send "y\r"
      exp_continue
   } "root*archiso*# " {
      send "lvcreate disk2 -L 16G -n terraswap2\r"
   }
}

注意:

或者,我们也可以使用大括号来匹配文字[

expect {\[}; # This too matches literal '['

答案 1 :(得分:0)

glob模式是隐式锚定的。您的模式不匹配,因为文本的完整与短模式不匹配。对于字符串包含"擦除它? [y / n]",你想:

function shallowClone(obj) {
    return Object.create(
        Object.getPrototypeOf(obj), 
        Object.getOwnPropertyDescriptors(obj) 
    );
}

请注意,{*Wipe it\? \[y/n\]:*} 是一个glob通配符,因此请将其转义为与文字问号相匹配。有关详细信息,请参阅string match文档。