Bash File Read Problem

I dont know why this trivial problem always seem to trip me up.

Here is a bash procedure called by calling it with the first entry in the file and the filename as arguments.

#!/bin/bash

    Procedure () {

        while IFS= read -r  f1 f2 f3
        do
    	echo "Procedure Read: $f1 $f2 $f3"
	echo "Value supplied at call to compare with first above = $1"
    	if [ "$f1" = "$1" ]; then	    
    	     echo "Comparison of  $f1=S1  Worked" ;
	else echo "Comparison of  $f1=$1  faled"; fi
        done < ./$2

    }
    	Procedure   "A"  testfile.dat;
    exit

The data file that is called by the Procedure is here.

testfile.dat contains only the three characters space separated in one line.
A B C

So why the heck doesnt the if condition work ?
It works great for variables in my program but as soon as I call characters from file it refuses.

[ “$f1” = “$1” ] just doesnt want to equate even though I test them both to be the same strings.
basically [ “A” = “A” ]

I am either getting old, or stupid…or both.

What is the purpose of if command? Are you trying match something? Say if $f1 contains A ($1) or not?

Yes the comparison above fails for the case if “f1” is read from a file and $1 is fed to the procedure call.

Simple as that. It is really elementary, but this trips me about once a year and comes in many flavors.
If “f1” is not read from file then it equates. I dont understand why the comparison fails when read from file but not if just hard set to the same value as the file.
Elementary issue that always bite me and I never get a logical reason how to circumvent such trivial issue.

Just create a file testfile.dat with a line containing A B C and run the script in same directory.
Apparently A!=A for some reason.
You should solve this in 10 seconds.
It must be something silly that trips me up.

I found the problem.
If you remove IFS= from the while statement;
while IFS= read -r f1 f2 f3
Then it works.

#!/bin/bash

Procedure () {

while read -r  f1 f2 f3
do
echo "Procedure Read: $f1 $f2 $f3"
echo "Value supplied at call to compare with first above = $1"
if [ "$f1" = "$1" ]; then	    
     echo "Comparison of  $f1=S1  Worked" ;
else echo "Comparison of  $f1=$1  faled"; fi
done < ./$2

}
Procedure “A” testfile.dat;
exit

I somehow misinterpreted the use of IFS again.

Oh well, brush up time on that again.
Sorry to have bothered you with something this trivial, but IFS seems to keep tripping me up.


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki