Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a file out.csv I have to check if the name inputed by user exists in the file(comma separated) or not. I am taking name using read but while checking for equality I am getting error

    IFS=","
    while read tname tnum
        do
            if [ $tname -eq $name ]; then
                flag=1
                break
            fi
        done < out.csv
    echo "$ch"
share|improve this question

1 Answer 1

up vote 6 down vote accepted

You're getting this error since you are trying to compare string using equality operators intended for integers, -eq, -ne, -gt, and similar are integer functions.

To compare strings use = to compare for equality OR != to compare for non-equality.

Check this for more on comparison operators.

if [ $tname -eq $name ]; then

should be changed to:

if [ "$tname" = "$name" ]; then

(also remember to quote your variables).

share|improve this answer
    
if [ $tname = $name ]; then also works what difference will it make if we add inverted commas(like "$tname") –  user2179293 Sep 8 '13 at 10:38
2  
@user2179293 Without quotes, the value of each variable is interpreted as a whitespace-separated list of glob patterns. With double quotes, the value of each variable is interpreted as a string. If you don't understand this, keep it simple: always put double quotes around variable substitutions, i.e. [ "$tname" = "$name" ]. Leave off the quotes only if you've read When is double-quoting necessary? and remember all the rules and hate the next person who will maintain that script. –  Gilles Sep 8 '13 at 23:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.