linux - 期望:检索命令的返回码

标签 linux expect

我有一个脚本可以登录服务器并执行一些命令。我需要能够从每个命令中检索返回代码,以确定脚本是否成功。我写了以下脚本,但没有按照我的意愿进行。目标是执行“cd/I/dont/exist”,这会产生错误代码“1”。使用正则表达式获取结果并将名为“result”的变量设置为该值。使用“result”判断cd命令通过与否,失败则退出。

目前它将执行所有必需的步骤,但正则表达式无法获取返回代码,而是报告一切正常。基于使用“exp_internal 1”,看起来它扫描了 expect_out 并在找到它应该找到的“1”之前找到了“0”。

我在这里错过了什么?

脚本:

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$ "

最佳答案

仔细看看这个:

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 命令后匹配您的提示。

关于linux - 期望:检索命令的返回码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51539319/

相关文章:

linux 找到 : move files by xargs

linux - 在 linux 服务器中为 apache 分配内存

expect - 我怎么知道我已经完成了交互模式?

linux - 自动结束等待您按 ENTER 的脚本

linux - 期望自动登录到二级远程机器

linux - 当您调用 select(2) 时,内核如何确定套接字已准备就绪?

python - "killall python"后看不到键盘输入

javascript - 期望脚本 : extra characters after close-brace while executing despite single quote marks

c - 获取打开设备的文件路径

java - 强制 read() 方法读取最少字符数