how to avoid 'unexpected operator' error when comparing 2 strings
this is my file:
The lol file contains 1aa and the lol1 file contains 1aa as well.
Unfortunately the output is
Even when I put -eq instead of == I get
How can I fix this?
Thank you in advance!
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.
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:
(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:
---------- 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.
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
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)
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)
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)
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)
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)
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)
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)
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)