Bash 不相等字串比較

Bash 不相等字串比較

為什麼這會導致無限循環?

#!/bin/bash
while [[ "$(ipcs | awk '{print $2}')" != "Semaphore" ]]; do
  #Gonna get rid of the echo after working
  echo "$(ipcs | awk '{print $2}')"
  #I want to keep this
  ipcrm shm "$(ipcs | awk '{print $2}')"
  #I want this run after breaking out of the loop until I reach the string
  #Message. 
  ipcrm -s "$(ipcs | awk '{print $2}')"
done      
echo
exit 0

我已經驗證我最終得到了 Semaphore,所以它應該跳出 while 循環。

$ echo $(ipcs | awk '{print $2}')
Shared shmid 262145 294914 2326531 Semaphore semid Message msqid


$ ipcs

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x00000000 262145     bob        600        393216     2          dest         
0x00000000 294914     bob        600        393216     2          dest         
0x00000000 2490371    bob        600        998400     2          dest         

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    

$ echo $(ipcs |  awk '{print $1}')
------ key 0x00000000 0x00000000 0x00000000 ------ key ------ key

$ echo $(ipcs |  awk '{print $2}')
Shared shmid 262145 294914 2490371 Semaphore semid Message msqid

答案1

$(ipcs | awk '{print $2}')永遠不等於Semaphore.它總是等於:

Shared shmid 262145 294914 2326531 Semaphore semid Message msqid

你可能想要這樣的東西:

for e in $(ipcs | awk '{print $2}'); do
    [[ "$e" = "Semaphore" ]] && break
    echo $e
done

echo
exit 0

您可能也喜歡這個awk解決方案:

ipcs | awk '$2 == "Semaphore" {exit;} $2 != "" {print $2}'

一點解釋:

  • 如果第二個欄位是訊號, 出口。
  • 否則,如果該字段不為空,則列印它。

以下是一些替代解決方案(假設我確實了解您的需求):

# List all shared memory segments keys
ipcs -m | awk 'NR > 3 && $1 != "" {print $1}'

# List all shared memory segments IDs
ipcs -m | awk 'NR > 3 && $2 != "" {print $2}'

對於每個範例,您都可以迭代結果:

for e in $(above command); do
    echo $e
done

答案2

這是處理 while 迴圈的稍微不同的方法:

$ while read line; do 
    echo "$line"; 
    if [[ "$line" != *Semaphore* ]]; then 
        echo "not semaphore"; 
    else 
        echo "is semaphore"; 
    fi;
  done < <(ipcs | awk '{print $2}')

產生以下輸出:

...
not semaphore
814972976
not semaphore
815005745
not semaphore
817070167
not semaphore

not semaphore
Semaphore
is semaphore
semid
not semaphore

請注意,當它到達字串“Semaphore”時,它會正確識別它。

相關內容