Bash not equal string comparison

Why does this cause an infinite loop?

#!/bin/bash


while [[ "$(ipcs | awk '{print $2}')" != "Semaphore" ]]; do
    echo "$(ipcs | awk '{print $2}')"

done
       
echo 
exit 0

I have verified I eventually get Semaphore so it should break out of the while loop.

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

Hi cokedude,
Could you please try following.

while [[ "$(ipcs | awk '{print $2}')" !~ "Semaphore" ]]; do
    echo "$(ipcs | awk '{print $2}')" 
done 
echo 
exit 0

Thanks,
R. Singh

For me what he tried looks right, but unfortunately he is comparing contents are not equal to a fixed string, so caught inside infinite loop.

---------- Post updated at 09:06 PM ---------- Previous update was at 09:04 PM ----------

Something like this should work I guess

#!/bin/bash


while [[ "$(ipcs | awk '{print $2}')" != *"Semaphore"* ]]; do
    echo "$(ipcs | awk '{print $2}')"

done
       
echo 
exit 0

I am bit confused with the contents

I tested like this, as soon as count reaches 500 it creates file and breaks while loop

#!/bin/bash

i=0
while [[ "$( ls )" != *"akshay"* ]]; do
	echo $i
	[ $i -eq 500 ] && touch unix.com_akshay_unix.com
i=$((i+1))
done

echo "released"

No need to use awk:-

#!/bin/bash

ipcs | while read v1 v2 skip
do
        [ "$v2" != "Semaphore" ] && echo "$v2" || break;
done

exit 0

This won't work :(.

$ bash ./whilee1
./whilee1: line 2: conditional binary operator expected
./whilee1: line 2: syntax error near `!~'
./whilee1: line 2: `while [[ "$(ipcs | awk '{print $2}')" !~ "Semaphore" ]]; do'

Is there a trick to get this to work?

$ while [[ "$(ipcs | awk '{print $2}')" != *"Semaphore"* ]]; do
>     echo "$(ipcs | awk '{print $2}')"
> 
> done


$ bash ./whilee1

The "trick" is to find out what the string is, not just what it looks like, so you can make a comparison which actually works.

I don't suppose you tried the while loop yet? It would be easiest to use that to check with, you could just print the value.

#!/bin/bash

ipcs | while read v1 v2 skip
do
        printf "Got [%s]\n" "$v2"

        [ "$v2" != "Semaphore" ] && echo "$v2" || break;
done

exit 0

Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.

(previous thread here)