簡體   English   中英

如何在expect shell腳本中獲取生成進程的退出代碼?

[英]How to get the exit code of spawned process in expect shell script?

我正在嘗試執行一個執行EXPECT腳本的腳本和一個包含退出代碼的衍生進程。 但我無法將生成的進程的退出代碼獲取到主腳本。 我總是因為成功而獲得零。

期望腳本是:

 [Linux Dev:anr ]$ cat testexit.sh
 #!/bin/bash
 export tmp_script_file="/home/anr/tmp_script_temp.sh"
 cp /home/anr/tmp_script $tmp_script_file
 chmod a+x $tmp_script_file
 cat $tmp_script_file
 expect << 'EOF'
 set timeout -1
 spawn  $env(tmp_script_file)
 expect {
 "INVALID "  { exit 4 }
 timeout     { exit 4 }
 }
 EOF
 echo "spawned process status" $?
 rm -f $tmp_script_file
 echo "done"

產生的腳本:

 [Linux Dev:anr ]$ cat tmp_script
 exit 3

執行Expect腳本:

 [Linux Dev:anr ]$ ./testexit.sh
 exit 3
 spawn /home/anr/tmp_script_temp.sh
 spawned process status 0
 done

問題是我無法獲得生成的退出返回代碼以期望腳本。 我希望生成的腳本的退出代碼3到主腳本,主腳本應該退出,退出代碼3。

請幫我將生成的退出代碼導入主腳本。

使用wait命令獲取生成進程的退出狀態:

expect <<'END'
log_user 0
spawn sh -c {echo hello; exit 42}
expect eof
puts $expect_out(buffer)

lassign [wait] pid spawnid os_error_flag value

if {$os_error_flag == 0} {
    puts "exit status: $value"
} else {
    puts "errno: $value"
}
END
hello

exit status: 42

來自期待的手冊頁

[args]

延遲直到生成的進程(或當前進程,如果沒有命名)終止。

等待通常會返回一個包含四個整數的列表。 第一個整數是等待進程的pid。 第二個整數是相應的spawn id。 如果發生操作系統錯誤,則第三個整數為-1,否則為0。 如果第三個整數為0,則第四個整數是生成進程返回的狀態。 如果第三個整數是-1,則第四個整數是操作系統設置的errno的值。 全局變量errorCode也已設置。


更改

expect {
"INVALID "  { exit 4 }
timeout     { exit 4 }
}

expect {
    "INVALID "  { exit 4 }
    timeout     { exit 4 }
    eof
}

然后添加lassignif命令。

在glenn的幫助下,我得到了解決方案..我的最終腳本是::

期待腳本是

 [Linux Dev:anr ]$ cat testexit.sh
 #!/bin/bash
 export tmp_script_file="/home/anr/tmp_script_temp.sh"
 cp /home/anr/tmp_script $tmp_script_file
 chmod a+x $tmp_script_file
 cat $tmp_script_file
 expect << 'EOF'
 set timeout -1
 spawn  $env(tmp_script_file)
 expect {
 "INVALID "  { exit 4 }
 timeout     { exit 4 }
 eof
 }

 foreach {pid spawnid os_error_flag value} [wait] break

 if {$os_error_flag == 0} {
     puts "exit status: $value"
     exit $value
 } else {
     puts "errno: $value"
     exit $value
 }
 EOF
 echo "spawned process status" $?
 rm -f $tmp_script_file
 echo "done"

產生的腳本:

 [Linux Dev:anr ]$ cat tmp_script
 exit 3

執行Expect腳本:

 [Linux Dev:anr ]$ ./testexit.sh
 exit 3
 spawn /home/anr/tmp_script_temp.sh
 exit status: 3
 spawned process status 3
 done

再次感謝格倫

經過幾天的努力,在預期的heredoc中擴展變量,最后我遇到了另一種我覺得可能對有需要的人有幫助的方法。 我的要求是將命令和密碼傳遞給shell函數,在遠程主機中執行命令作為期望heredoc的一部分並獲取返回退出代碼。

例:

function shell_function {
   # Get the command and password as arguments
   # Run command using expect
   # Return the exit code
}

shell_function <cmd> <password>
echo $?

像其他人一樣,在heredoc中擴展變量是一個問題,需要將值導出到環境變量中並使用env來獲取heredoc中的變量。 因為,密碼是我不想將其作為環境變量的一部分存儲的參數之一。 因此,不是用單引號括起heredoc開頭,而是轉義了heredoc的變量。 這允許直接使用傳遞的參數。

以下是最終腳本:

#! /bin/bash
# This function runs a command like 'ssh' and provides the password
function run_with_password {
    cmd="$2"
    paswd="$1"
    expect << END
        set timeout 60
        spawn $cmd
        expect {
            "yes/no" { send "yes\r" }
            "*assword*" { send -- $paswd\r }
        }
        expect EOF
        catch wait result
        exit [lindex \$result 3]
END
}

my_password="AnswerIS42Really?"
cmd_to_run="ssh userid@hostname"
cmd_to_run="$cmd_to_run ls .sawfish"
run_with_password $my_password "$cmd_to_run"
echo "Command run code: $?"

在上面的代碼中,轉義的期望變量是$result 將變量更改為\\$result ,腳本開始像charm一樣工作。

衷心感謝為以下問題提供答案的用戶,這些問題是我達成解決方案的墊腳石。

Douglas Leeder:幫助期望腳本,在遠程comp上運行cat並將其輸出到變量

glenn jackman:如何在Expect腳本中返回生成的進程退出代碼?

暫無
暫無

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

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