Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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.

share|improve this question
    
Variables surrounded by single quotes are not evaluated. Use double quotes. – Etan Reisner Jul 23 '14 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. – Etan Reisner Jul 23 '14 at 21:46
    
Look into $IFS — the Internal Field Separator. This is used by read (and many other programs) to separate text strings. – baum Jul 23 '14 at 21:51
    
i do have the IFS="," in the script. If i echo $version, it displays 5.9 – user3870602 Jul 23 '14 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. – Costi Ciudatu Jul 23 '14 at 22:37

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
share|improve this answer

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.
share|improve this answer

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
share|improve this answer

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.