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”时,它会正确识别它。

相关内容