0

I have a csv file that i am reading with a "while read" statement and i want to run an if statement on one of the fields in the csv.

====================================

csv file

client1,admin,password,5.9  
client2,admin,password,5.8  

====================================

this is my script


while read clientid user pass version  
do  
    if [ '$version' = "5.9" ];  
    then  
        echo "IS"  
    else  
        echo "NOT"  
    fi  
done < $1  

The problem is that the if statement does not work. It does not echo IS when the version is 5.9, it just keeps saying NOT, unless i change it to !=
I have tried using single and double quotes, even without... still doesn't work as expected.

The goal is to run commands until the end of the file. Is this script correct for doing this?

Obviously the IS and NOT would be replaced by actual command, this is just for testing.

8
  • Variables surrounded by single quotes are not evaluated. Use double quotes. Commented Jul 23, 2014 at 21:46
  • Also read will not split on commas by default. You need to tell it to do that. Print out the value of $clientid, $user, $pass, and $version in your loop to see what you get. Commented Jul 23, 2014 at 21:46
  • Look into $IFS — the Internal Field Separator. This is used by read (and many other programs) to separate text strings. Commented Jul 23, 2014 at 21:51
  • i do have the IFS="," in the script. If i echo $version, it displays 5.9 Commented Jul 23, 2014 at 22:02
  • @baum: can you provide a working example script with IFS=, or read -d,? I'm getting really weird output when using this. Commented Jul 23, 2014 at 22:37

3 Answers 3

1

The sample csv file provided has trailing whitespace on the line, which can be removed from the version variable using parameter expansion.

This should work:

while IFS=, read -r clientid user pass version; do
    if [ "${version//[[:space:]]/}" = "5.9" ]; then
        echo "IS"
    else
        echo "NOT"
    fi
done < $1
Sign up to request clarification or add additional context in comments.

Comments

1

And here's another:

while IFS=$' \t\r\n' read -r line; do
    IFS=, read -r clientid user pass version __ <<< "$line"
    if [[ $version == '5.9' ]]; then
        echo "IS"
    else
        echo "NOT"
    fi
done < "$1"
  • Quote variables in the open always to prevent word splitting and pathname expansion.
  • Prefer [[ ]] over [ ]. It doesn't do word splitting and pathname expansion.
  • IFS=$' \t\r\n' trims out leading and trailing spaces.
  • __ is added to store surplus values just in case.

Comments

1

You can add the IFS value comma and whitespace IFS=', ' . You will get the exact result.

#!/bin/bash
IFS=', '
while read clientid user pass version  
do
    if [ "$version" == "5.9" ] ; then
        echo "IS"  
    else
        echo "NOT"  
    fi
done < $1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.