簡體   English   中英

Bash Shell Do While Loop Infinite循環?

[英]Bash Shell Do While Loop Infinite loop?

基本上這是我的代碼:

bay=$(prog -some flags)
while [ $bay = "Another instance of this program is running, please exit it first" ]
do
echo "Awaiting Access to program"
do
.....

我有一個程序,它只允許一個實例一次運行,因為它與我的硬件交互的方式,當另一個實例運行時它踢出以下消息“該程序的另一個實例正在運行,請先退出它” 。

我需要能夠運行多個腳本,這將使用相同的程序,所以我決定使用上面的代碼。 我的問題是,當我運行我的兩個腳本時,一個人將獲得對程序的訪問並按照需要運行,但另一個將注意到錯誤,然后卡在一個無限循環中,回顯“等待訪問程序”。

錯過了什么? Statement是執行CLI命令還是只是重新執行其原始執行? 或者我的問題在哪里?

您沒有在某個地方更新循環內的bay變量。 它被設置一次並保持不變。 你需要每次都重新計算它。

在循環內設置bay ,或者在while的條件下設置bay

while [ `prog -some flags` = "Another instance of this program is running, please exit it first" ]

編輯:

從您的評論中,您希望以后能夠引用此輸出。 你可以回到你擁有的東西,但是在你的阻塞循環中,把你的bay=$(prog -some flags)命令放在循環中。 它會留在你身邊供你使用。

bay=$(prog -some flags)
while [ $bay = "Another instance of this program is running, please exit it first" ]
do
echo "Awaiting Access to program"
bay=$(prog -some flags)
done
.....

更多 ,而不是錘擊前衛,我會等待用戶先做一些事情:

while true
do
  bay=$(prog -some flags)
  case "$bay" in
    "Another instance of this program is running, please exit it first")
      read -p "Awaiting Access to program. Close it and hit enter: " x ;;
    *) break ;;
  esac
done
echo "Results: $bay"

暫無
暫無

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

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