gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:07:14 24 4
gpt4 key购买 nike

我有一个脚本可以登录服务器并执行一些命令。我需要能够从每个命令中检索返回代码,以确定脚本是否成功。我写了以下脚本,但没有按照我的意愿进行。目标是执行“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com