how to avoid 'unexpected operator' error when comparing 2 strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to avoid 'unexpected operator' error when comparing 2 strings
# 1  
Old 08-07-2010
Tools how to avoid 'unexpected operator' error when comparing 2 strings

this is my file:
Code:
#!/bin/sh
a=`cat /home/$USER/Desktop/lol`
c=`cat /home/$USER/Desktop/lol1`
if [ "$a" == "$c" ]; then
echo "$a = $c"
else
echo "They are not equal"
fi

The lol file contains 1aa and the lol1 file contains 1aa as well.
Unfortunately the output is
Code:
[: 8: 1aa: unexpected operator
They are not equal

Even when I put -eq instead of == I get
Code:
[: 8: Illegal number: 1aa
They are not equal

How can I fix this?
Thank you in advance!
# 2  
Old 08-07-2010
Use = instead of == ... and perhaps prepend each operand with a constant prefix:
Code:
 [ X"$a" = X"$c" ]

# 3  
Old 08-07-2010
thank you very much. What is the use of the 'X' you placed there?
# 4  
Old 08-07-2010
The "X" either side of the equals sign allows for either $a or $c to be empty strings.
Afaik the "==" is only valid in bash shell. Use "=" as alister suggests.
The "-eq" operator only applies to numbers not strings.
# 5  
Old 08-07-2010
The test command is what is being invoked to process the expression between the square brackets. If either variable evaluates to nothing (in this case one of your files is empty), then the expression that the test command sees is:

Code:
    stuff =

(nothing trailing the operator if $c was empty) and this is not a legit expression.

Placing an 'x' in front of each variable ensures that the expression does not have any missing components, yet does not change the outcome of the string comparison because both strings will start with the same character.

Shells like Kshell which have a built-in test command (e.g. [[ $a == $b ]]) recognise an empty variable and do the right thing because the expression is being evaluated internally. So, in these scripts you will not encounter the 'x-trick' as it's not needed.

As for your method of cat'ing the files into variables might not be the most efficient way to accomplish your task. I'd use the diff command in this manner:

Code:
if diff -q file1 file2 >/dev/null 2>&1
then
    echo "match"
else
    echo "mismatch"
fi



---------- Post updated at 04:35 PM ---------- Previous update was at 04:29 PM ----------

Quote:
Originally Posted by methyl
Afaik the "==" is only valid in bash shell.
Yes, Bash and Kshell support the == operator, but only within their built-in test function ([[....]]). If a bash/Kshell script uses the single bracket command the contents in the expression are treated differently by the shell and the double equal operator is not valid.

Code:
#/usr/env/bin ksh
a=foo
b=bar
if [[ $a == $b ]]     # this is legit
then 
    echo true
else
    echo false
fi 

if [ $a == $b ]       # this will cause an error
then 
   echo true 
else
   echo false
fi

# 6  
Old 08-07-2010
His test expression will never be missing any components even if $a and $c are unset or null, since they are quoted. The test command (whether built-in or not) will see empty strings in their place.

The actual reason for the "x-trick" is that historical implementations are vulnerable to situations where one of the tested values ("$a" and "$c" in this situation) expand into valid test operators. Not knowing what platform the poster is on, I thought it best to be safe.

For more info, see the APPLICATION USAGE section @ test

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 08-07-2010
i saw the manual of diff and I find it very interesting. Unfortunately I didn't understand what you did in here:
Code:
if diff -q /home/alex/Desktop/lol /home/alex/Desktop/lol1 >/dev/null 2>&1
echo "match"
else
echo "mismatch"
fi

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Strings comparing incorrectly

Hello I'm very new to Linux and shell scripting so I only know basic stuff. I'm making a script with the purpose of finding the longest string or word in a file. Here's what I got so far: #!/bin/bash longest="" for i in $(strings -n $1); do if ] then longest=$i fi done echo $longest... (4 Replies)
Discussion started by: SCB
4 Replies

2. Shell Programming and Scripting

ksh-script "arithmetic syntax error" comparing strings

Hi all, Iīve already searched the forum but canīt find what i am doing wrong. I am trying to compare two variables using ksh under red hat. The error I get is: -ksh: .: MDA=`md5sum /tmp/ftp_dir_after_transfer | cut -d' ' -f1 ` MDB=`md5sum /tmp/ftp_dir_before_transfer | cut -d' ' -f1 `... (3 Replies)
Discussion started by: old_mike
3 Replies

3. UNIX for Dummies Questions & Answers

How do I search for 2 strings (AND operator) ?

I have a need to search for files containing 2 strings as in (AND operator). No one at my site seems to know if it is possible. There is only documentation for the "or' operator. I know I can do a search, copy all the matched files into a temp directory & do the second search in the temp... (14 Replies)
Discussion started by: Kartheg
14 Replies

4. Shell Programming and Scripting

syntax error: `-a' unexpected operator/operand in IF

When i tyr this, it gives me a syntax error...i tried removing quotes,removing spaces,replacing -eq with '='.. Can somebody suggest that is the problem? if ]; then (4 Replies)
Discussion started by: dba.admin2008
4 Replies

5. Shell Programming and Scripting

comparing two strings

hi All i am facing prob in comparing two strings that have two word. below is the code snippet. checkValidates="file validates" file3_name="file" if then echo "file" $file3_name "is validated successfully" fi when i run this i get the error as -bash: [: too many arguments ... (1 Reply)
Discussion started by: infyanurag
1 Replies

6. Shell Programming and Scripting

comparing 2 strings

hi i have 2 strings. i want to compare the strings. please help (2 Replies)
Discussion started by: satish@123
2 Replies

7. Shell Programming and Scripting

comparing strings

i have a string in a file which gets repeated number of times like below: rpttxt("abc") . . rpttxt("REP_TITLE") rpttxt("BOS_TITLE") . . . . and so on using awk or grep how can i comapre the string( as the second half keeps varying) and store it in a temporary variable? I am using the... (3 Replies)
Discussion started by: agarwal
3 Replies

8. Shell Programming and Scripting

Comparing Two Strings

Hi All, While I am trying to run below code I Am getting the exception like ./abs.sh: line 102: syntax error near unexpected token `then' ./abs.sh: line 102: ` then' The Code Snippet is: if then cat $file1 | sed -e... (8 Replies)
Discussion started by: Anji
8 Replies

9. UNIX for Advanced & Expert Users

Comparing strings

I have two strings a=Mar22 b=may21 how can I compare them Is this fine if then; . ... else .... fi or if then (2 Replies)
Discussion started by: yakyaj
2 Replies

10. Shell Programming and Scripting

comparing two strings

Hi How do i compare two strings in shell script. Below is an example but I am not getting the desired output, plz help if then echo success fi I am not getting the desired output if I do this. plz help (24 Replies)
Discussion started by: ragha81
24 Replies
Login or Register to Ask a Question