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
# 8  
Old 08-07-2010
If the program returns success, it prints match. If it returns error, it prints mismatch. The rest is redirection to shunt all the unneeded output into /dev/null where it will go away and never be heard from again.
# 9  
Old 08-09-2010
@agama

The "==" is not valid in standard unix "ksh" , and your example gives "Syntax error at line 4 : `==' is not expected.".

I have also checked Bourne Shell and Posix Shell. Still invalid.
However it might be valid in a Conditional Expression in ksh93 ?

The O/P is using "/bin/sh" which could be Bourne Shell or Posix Shell depending on what Operating System version we have here. We can deduce that it is not "bash" or "ksh". Safest to assume Bourne Shell


The rules:

Between single square brackets [...] the expression is evaluated according the the rules for "test" (or the shell emulation of "test").

Between double square brackets [[...]]the expression is evaluated according to the rules for Conditonal Expressions (a separate section of the "ksh" manual).

There is some overlap in the syntax for "test" and a "Conditional Expression" but in standard unix "ksh" none of them include "==".



On the subject of "diff". The "diff -q" syntax is not valid in standard unix. I think you'll find the equivalent "cmp -s" in most of versions.

This is developing into another classic thread where nobody has the same Operating System yet everybody assumes that unix/Linix commands are universal when they are not.
# 10  
Old 08-09-2010
cmp would be more efficient.
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