Bash not equal string comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash not equal string comparison
# 1  
Old 10-09-2014
Bash not equal string comparison

Why does this cause an infinite loop?

Code:
#!/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.

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

# 2  
Old 10-09-2014
Quote:
Originally Posted by cokedude
Why does this cause an infinite loop?

Code:
#!/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.

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

Hi cokedude,
Could you please try following.

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

Thanks,
R. Singh
# 3  
Old 10-09-2014
Quote:
Originally Posted by RavinderSingh13
Hi cokedude,
Could you please try following.

Code:
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

Code:
#!/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

Quote:
Originally Posted by cokedude
Why does this cause an infinite loop?

Code:
#!/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.

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

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

Code:
#!/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"


Last edited by Akshay Hegde; 10-09-2014 at 12:06 PM.. Reason: -- confused with the contents shown, after using awk '{ print $2 }'--
# 4  
Old 10-09-2014
No need to use awk:-
Code:
#!/bin/bash

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

exit 0

# 5  
Old 10-09-2014
Quote:
Originally Posted by RavinderSingh13
Hi cokedude,
Could you please try following.

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

Thanks,
R. Singh
This won't work Smilie.

Code:
$ 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'

Quote:
Originally Posted by Akshay Hegde
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

Code:
#!/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

Code:
#!/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"

Is there a trick to get this to work?

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


$ bash ./whilee1

# 6  
Old 10-09-2014
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.

Code:
#!/bin/bash

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

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

exit 0

# 7  
Old 10-09-2014
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)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk string comparison unterminated quoted string andrule of thumb

I have the logic below to look up for matches within the columns between the two files with awk. In the if statement is where the string comparison is attempted with == The issue seems to be with the operands, as 1. when " '${SECTOR}' " -- double quote followed by single quote -- awk matches... (1 Reply)
Discussion started by: deadyetagain
1 Replies

2. Shell Programming and Scripting

AWK splitting a string of equal parts of 500 chars

Hi , can someone help me how to make an AWK code for splitting a string of equal parts of 500 chars in while loop? Thank you! (4 Replies)
Discussion started by: sanantonio7777
4 Replies

3. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

4. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

How to compare particular string, if it is equal put into a new file

Hi, I have a file (sample.txt) this file contains below text. ./au ./11.5.0 ./11.5.0/admin ./11.5.0/admin/driver ./po ./11.5.0 ./11.5.0/admin ./11.5.0/admin/driver ./xxsbx/11.5.0/java/src ./xxsbx/11.5.0/lib ./xxsel ./xxsel/11.5.0 ./xxsel/11.5.0/admin ./zfa ./zfa/11.5.0... (2 Replies)
Discussion started by: gagan4599
2 Replies

6. Shell Programming and Scripting

String comparison using bash

I have a program to create a directory if not present. Here is the program. FYI: Directory name format: YYYY_MM_DD #!/bin/bash date=`date +%Y_%m_%d` presence=$(ls -lrt /TS_File/ | grep "$date" | awk '{print $9}') if then mkdir /TS_File/$date else echo "Unable to Create... (5 Replies)
Discussion started by: nthiruvenkatam
5 Replies

7. Shell Programming and Scripting

Bash Script verify user input is not empty and is equal to a value

I need to create a script that has a user enter a value. I want to verify that the value is either 1,2, or 3. If it is not then I want them to try entering it again. I am using a while loop to force them to retry. I am able to test the input against 1,2, and 3, but when I test agains an... (4 Replies)
Discussion started by: spartiati
4 Replies

8. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

9. Shell Programming and Scripting

date comparison in bash

Hi I have this simple script: #!/bin/bash date1=2009:07:15:12:36 date2=2009:07:15:12:16 echo $date1 echo $date2 datediff= #datediff=date1-date2 echo datediff is$datediff How do i return the difference in seconds? (6 Replies)
Discussion started by: carp.dk
6 Replies

10. Shell Programming and Scripting

bash shell script string comparison

I want to remove a line that has empty string at second field when I use cut with delimeter , like below $cat demo hello, mum hello, #!/bin/sh while read line do if then # remove the current line command goes here fi done < "demo" i got an error message for above... (4 Replies)
Discussion started by: bonosungho
4 Replies
Login or Register to Ask a Question