期望:检索命令的返回码

时间:2018-07-26 12:52:58

标签: linux expect

我有一个登录到服务器并执行一些命令的脚本。我需要能够从每个命令中检索返回代码,以便确定脚本是否成功。我写了下面的脚本,但并没有达到我的期望。目标是执行“ cd / I / dont / exist”,它将产生错误代码“ 1”。使用正则表达式获取结果,然后将名为“结果”的变量设置为该值。使用“结果”来确定cd命令的通过或失败,如果失败,则退出。

当前它将执行所有必需的步骤,但是正则表达式无法获取返回代码,而是报告一切正常。基于使用“ exp_internal 1”,它看起来像遍历了expected_out并找到了“ 0”,然后才找到了应该的“ 1”。

我在这里错过了什么?

脚本:

spawn bash
expect "$ "
send "cd /I/dont/exist\r"

#clear expect buffer
sleep 1
expect *
sleep 1

#get return code and validate it
send "echo \$?\r"
expect -re "(\\d+)" {
     set result $expect_out(1,string)
}

if { $result != 0 } {
        puts "Got result $result which is not 0"
        exit $result
} else {
        puts "didn't have a problem, $result is 0"
}

expect "$ "

当前输出:

user@mycomputer:dir/scripts$ expect expect_script.exp 
spawn bash
user@mycomputer:dir/scripts$ cd /I/dont/exist
bash: cd: /I/dont/exist: No such file or directory
user@mycomputer:dir/scripts$ echo $?
1
didn't have a problem, 0 is 0
user@mycomputer:dir/scripts$

预期输出:

user@mycomputer:dir/scripts$ expect expect_script.exp 
spawn bash
user@mycomputer:dir/scripts$ cd /I/dont/exist
bash: cd: /I/dont/exist: No such file or directory
user@mycomputer:dir/scripts$ echo $?
1
Got result 1 which is not 0
user@mycomputer:dir/scripts$

输出为exp_internal 1:

user@mycomputer:/dir/scripts$ expect expect_script.exp 
spawn bash
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {20801}

expect: does "" (spawn_id exp4) match glob pattern "$ "? no
user@mycomputer:/dir/scripts$ 
expect: does "\u001b]0;user@mycomputer: /dir/scripts\u0007\u001b[01;32muser@mycomputer\u001b[00m:\u001b[01;34m/dir/scripts\u001b[00m$ " (spawn_id exp4) match glob pattern "$ "? yes
expect: set expect_out(0,string) "$ "
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "\u001b]0;user@mycomputer: /dir/scripts\u0007\u001b[01;32muser@mycomputer\u001b[00m:\u001b[01;34m/dir/scripts\u001b[00m$ "
send: sending "cd /I/dont/exist\r" to { exp4 }

expect: does "" (spawn_id exp4) match glob pattern "*"? yes
expect: set expect_out(0,string) ""
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) ""
send: sending "echo $?\r" to { exp4 }
Gate keeper glob pattern for '(\d+)' is ''. Not usable, disabling the performance booster.

expect: does "" (spawn_id exp4) match regular expression "(\d+)"? (No Gate, RE only) gate=yes re=no
cd /I/dont/exist
bash: cd: /I/dont/exist: No such file or directory
user@mycomputer:/dir/scripts$ echo $?
1

expect: does "cd /I/dont/exist\r\nbash: cd: /I/dont/exist: No such file or directory\r\n\u001b]0;user@mycomputer: /dir/scripts\u0007\u001b[01;32muser@mycomputer\u001b[00m:\u001b[01;34m/dir/scripts\u001b[00m$ echo $?\r\n1\r\n" (spawn_id exp4) match regular expression "(\d+)"? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "0"
expect: set expect_out(1,string) "0"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "cd /I/dont/exist\r\nbash: cd: /I/dont/exist: No such file or directory\r\n\u001b]0"
didn't have a problem, 0 is 0

expect: does ";user@mycomputer: /dir/scripts\u0007\u001b[01;32muser@mycomputer\u001b[00m:\u001b[01;34m/dir/scripts\u001b[00m$ echo $?\r\n1\r\n" (spawn_id exp4) match glob pattern "$ "? yes
expect: set expect_out(0,string) "$ "
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) ";user@mycomputer: /dir/scripts\u0007\u001b[01;32muser@mycomputer\u001b[00m:\u001b[01;34m/dir/scripts\u001b[00m$ "

1 个答案:

答案 0 :(得分:1)

仔细看看:

expect: does "cd /I/dont/exist\r\nbash: cd: /I/dont/exist: No such file or directory\r\n\u001b]0;user@mycomputer: /dir/scripts\u0007\u001b[01;32muser@mycomputer\u001b[00m:\u001b[01;34m/dir/scripts\u001b[00m$ echo $?\r\n1\r\n" (spawn_id exp4) match regular expression "(\d+)"? (No Gate, RE only) gate=yes re=yes

您的提示中带有颜色代码,因此可以从提示本身中找到0

"\u001b]0;user@mycomputer: /dir/scripts\u0007\u001b[01;32muser@mycomputer\u001b[00m:\u001b[01;34m/dir/scripts\u001b[00m$ "
........^

您要匹配显示在自己行上的数字:

expect -re {\r\n(\d+)\r\n} {set result $expect_out(1,string)}

此外,expect *与空字符串匹配。您要在发送cd命令后匹配提示符。

相关问题