期待:コマンドの戻りコードを取得します

マイケルハックマン

サーバーにログインしていくつかのコマンドを実行するスクリプトがあります。スクリプトが成功したかどうかを判断するには、これらの各コマンドからリターンコードを取得できる必要があります。次のスクリプトを作成しましたが、期待どおりに動作していません。目標は、エラーコード「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]

編集
0

コメントを追加

0

関連記事

分類Dev

mysqlコマンドの戻りコードを取得します

分類Dev

SFTPコマンドの戻りコードを取得する

分類Dev

短絡式でコマンドの戻りコードを取得する方法

分類Dev

以前の関数から戻りコードを取得します

分類Dev

コマンドを実行し、C ++でコマンドの戻りコードstdoutおよびstderrを取得する方法

分類Dev

pipe andteeコマンドを使用してバックグラウンドで実行されたコマンドの戻り値を取得します

分類Dev

コマンド「mv-n」の戻り値を取得するlinux

分類Dev

コマンドの戻り値を取得する

分類Dev

Bashはコマンドの戻り値を取得し、この値で終了します

分類Dev

ajax関数のコードビハインドから戻り値を取得します

分類Dev

元に戻す/やり直しでコマンドパターンの履歴をシフトしますか?

分類Dev

AWKの最後のコマンドの戻りコードにアクセスします

分類Dev

コマンド「exec」から終了コードを取得します

分類Dev

コマンドPIDを取得し、別のコマンドに送信します

分類Dev

Pythonのシェルコマンドから戻り値を取得する

分類Dev

bashコマンドクラビング:特定のコマンドの終了ステータスを取得します

分類Dev

evalを使用して最初のパイプコマンドのリターンコードを取得しますか?

分類Dev

jQueryを使用してDOMノードを取得し、コンマ区切りの文字列に出力します

分類Dev

Vimで外部コマンドの戻り(ステータス)値を取得する方法

分類Dev

各sudoコマンドで、rootの[sudo]パスワードを取得します。

分類Dev

Vimはctagsの代わりにCscopeコマンドを取得しますか?

分類Dev

元に戻すコマンドとやり直しコマンドを使用してRichTextBoxツールバーの色を変更する

分類Dev

関数の完了後にコードを実行し、戻り値を取得する

分類Dev

stdinを使用する場合のコマンドの戻り値?

分類Dev

stdinを使用する場合のコマンドの戻り値?

分類Dev

記憶/コマンドパターンを使用した高速の元に戻す/やり直し?

分類Dev

NPCD:エラー:実行されたコマンドは戻りコード「255」で終了します

分類Dev

ReactコンポーネントのESLintルール:メソッドレンダーは戻り値を期待していません

分類Dev

ArrayListsの元に戻すとやり直しにコマンドパターンを使用する

Related 関連記事

  1. 1

    mysqlコマンドの戻りコードを取得します

  2. 2

    SFTPコマンドの戻りコードを取得する

  3. 3

    短絡式でコマンドの戻りコードを取得する方法

  4. 4

    以前の関数から戻りコードを取得します

  5. 5

    コマンドを実行し、C ++でコマンドの戻りコードstdoutおよびstderrを取得する方法

  6. 6

    pipe andteeコマンドを使用してバックグラウンドで実行されたコマンドの戻り値を取得します

  7. 7

    コマンド「mv-n」の戻り値を取得するlinux

  8. 8

    コマンドの戻り値を取得する

  9. 9

    Bashはコマンドの戻り値を取得し、この値で終了します

  10. 10

    ajax関数のコードビハインドから戻り値を取得します

  11. 11

    元に戻す/やり直しでコマンドパターンの履歴をシフトしますか?

  12. 12

    AWKの最後のコマンドの戻りコードにアクセスします

  13. 13

    コマンド「exec」から終了コードを取得します

  14. 14

    コマンドPIDを取得し、別のコマンドに送信します

  15. 15

    Pythonのシェルコマンドから戻り値を取得する

  16. 16

    bashコマンドクラビング:特定のコマンドの終了ステータスを取得します

  17. 17

    evalを使用して最初のパイプコマンドのリターンコードを取得しますか?

  18. 18

    jQueryを使用してDOMノードを取得し、コンマ区切りの文字列に出力します

  19. 19

    Vimで外部コマンドの戻り(ステータス)値を取得する方法

  20. 20

    各sudoコマンドで、rootの[sudo]パスワードを取得します。

  21. 21

    Vimはctagsの代わりにCscopeコマンドを取得しますか?

  22. 22

    元に戻すコマンドとやり直しコマンドを使用してRichTextBoxツールバーの色を変更する

  23. 23

    関数の完了後にコードを実行し、戻り値を取得する

  24. 24

    stdinを使用する場合のコマンドの戻り値?

  25. 25

    stdinを使用する場合のコマンドの戻り値?

  26. 26

    記憶/コマンドパターンを使用した高速の元に戻す/やり直し?

  27. 27

    NPCD:エラー:実行されたコマンドは戻りコード「255」で終了します

  28. 28

    ReactコンポーネントのESLintルール:メソッドレンダーは戻り値を期待していません

  29. 29

    ArrayListsの元に戻すとやり直しにコマンドパターンを使用する

ホットタグ

アーカイブ