Bash: integer expression expected

bashscriptingshell-script

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"

Best Answer

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).

Related Question