簡體   English   中英

如何將退出狀態從Expect傳播到其父Bash腳本?

[英]How can I propagate an exit status from Expect to its parent Bash script?

我有一個bash腳本,exec.sh之類的

some command
expect test.exp
continue other command

在test.exp文件中,我有一個代碼片段:

while {[gets $cmds command]>=0} {
  send "$command\r"
  expect {
    "*OK*" {puts $vout $command}
    "*fail*" {puts $iout $command}
    "*blocked*" { what should I put here????}
    }  
  }

所以我想把一些東西放在花括號中,以便執行退出test.exp並發出信號bash腳本exec.sh,所以exec.sh也退出我的想法是設置一個外部變量然后在exec.sh中使用“如果“判斷聲明

有什么想法嗎? 謝謝!

從Expect傳遞退出狀態

Tcl(因此Expect)有一個帶有參數的exit命令。 參數是流程的退出狀態。 您可以為退出狀態指定含義,並從shell腳本測試退出狀態。 例如,使用/usr/include/sysexits.h中的值,您可以編寫:

expect {
  "blocked" { exit 69 }
}

然后在腳本中測試該值。

Shell中的退出狀態分支

最后一個進程的退出狀態存儲在$? 變量。 測試這種方法的一種方法是使用case語句,並相應地進行分支。 例如:

expect test.exp
case $? in
  69)
    # Handle the exit status, and then propagate the same exit status
    # from the shell script.
    echo 'service unavailable' > /dev/stderr
    exit 69
    ;;
esac

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM