サーバーにログインしていくつかのコマンドを実行するスクリプトがあります。スクリプトが成功したかどうかを判断するには、これらの各コマンドからリターンコードを取得できる必要があります。次のスクリプトを作成しましたが、期待どおりに動作していません。目標は、エラーコード「1」を生成する「cd / I / dont / exist」を実行することでした。正規表現を使用して結果を取得し、「result」という変数をその値に設定します。「結果」を使用してcdコマンドの合否を判断し、失敗した場合は終了します。
現在、必要なすべての手順を実行しますが、正規表現は戻りコードを取得できず、代わりにすべてが正常であると報告します。「exp_internal1」の使用に基づくと、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
コマンドを送信した後、プロンプトを照合する必要があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加