第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從命令記錄退出代碼

從命令記錄退出代碼

Go
哆啦的時光機 2022-10-24 09:40:39
我正在嘗試使用以下代碼行在 go 中運行命令。    cmd := exec.Command(shell, `-c`, unsliced_string)      cmd.Stderr = os.Stderr     cmd.Stdout = os.Stdout     cmd.Stdin = os.Stdin     cmd.Run()變量 shell 是從 os.Getenv("$SHELL") 收集的,變量 unsliced_string 是從命令行提供的參數(shù)。我需要命令運行后的狀態(tài)/錯誤代碼。因此,如果正在運行的命令(來自命令)是exit 100,我需要一個保存錯誤狀態(tài)代碼的變量,在本例中為 100總的來說,我需要一個變量來記錄命令運行的錯誤代碼我嘗試過使用 .Error() 但是它exit status 100不僅僅是100 作為最后的手段,我可以使用 strings.Replaceall 或 strings.Trim
查看完整描述

1 回答

?
慕容708150

TA貢獻1831條經(jīng)驗 獲得超4個贊

當然,有兩種方法:


cmd := exec.Command(shell, `-c`, unsliced_string) 

err := cmd.Run()

if exitErr, ok := err.(*exec.ExitError); ok {

    exitCode := exitErr.ExitCode()

    fmt.Println(exitCode)

} else if err != nil {

    // another type of error occurred, should handle it here

    // eg: if $SHELL doesn't point to an executable, etc...

}

cmd := exec.Command(shell, `-c`, unsliced_string) 

_ := cmd.Run()

exitCode := cmd.ProcessState.ExitCode()

fmt.Println(exitCode)

我強烈建議您使用第一個選項,這樣您就可以捕獲所有exec.ExitError's 并按照您的意愿處理它們。如果命令沒有退出或者底層命令由于另一個錯誤而永遠不會運行,則不會填充 cmd.ProcessState ,因此使用第一個選項更安全。


查看完整回答
反對 回復 2022-10-24
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號