I guess instead of looking into Scrutinizer's suggested script, you want an explanation of why your script is not working.
I'll start out with your script and csv file that has only the first two records. I've added a couple of "echo" statements in your script to aid in debugging. They are shown in red below.
$
$ cat -n processnames.sh
1 echo "Name:"
2 read Name
3
4 echo "Value of Name : ==>$Name<=="
5 prev=`cut -d ";" -f1-3 funcionarios.csv`
6 echo "Value of prev : ==>$prev<=="
7
8 for linha in "$prev"
9 do
10 echo "Value of linha : ==>$linha<=="
11 fun=`echo "$linha" | cut -d ";" -f2`
12 echo "Value of fun : ==>$fun<=="
13 if [ "$Name" = "$fun" ]; then
14 echo "It's the same"
15 fi
16 done
$
$
The csv file looks like this -
$
$ cat -n funcionarios.csv
1 001op;Jose Antonio Borges;Rua de Cima, 7, Lisboa
2 002op;Silvia Maria;xxxx xxxx, 8, Lisboa
$
$
Now run the script. My shell is Bash, btw.
$
$ . processnames.sh
Name:
Jose Antonio Borges
Value of Name : ==>Jose Antonio Borges<==
Value of prev : ==>001op;Jose Antonio Borges;Rua de Cima, 7, Lisboa
002op;Silvia Maria;xxxx xxxx, 8, Lisboa<==
Value of linha : ==>001op;Jose Antonio Borges;Rua de Cima, 7, Lisboa
002op;Silvia Maria;xxxx xxxx, 8, Lisboa<==
Value of fun : ==>Jose Antonio Borges
Silvia Maria<==
$
$
Here's what happened.
(i) I entered the name "Jose Antonio Borges".
(ii) It printed the value of name "Jose Antonio Borges" at line 4.
(iii) It fetches value of prev at line 5.
(iv) It prints the value of prev at line 6. Note this value of prev:
Value of prev : ==>001op;Jose Antonio Borges;Rua de Cima, 7, Lisboa
002op;Silvia Maria;xxxx xxxx, 8, Lisboa<==
Is this what you wanted ??
Or did you want the value "001op;Jose Antonio Borges;Rua de Cima, 7, Lisboa" while processing line 1.
And "002op;Silvia Maria;xxxx xxxx, 8, Lisboa" while processing line 2.
If that's the case, then you don't have to do that. It's an unnecessary and convoluted way of doing things. You don't have to split the line twice, you can read the line and split it into tokens while reading itself. Which is what Scrutinizer is trying to tell you.
(v) The value of "linha" is the same as "prev".
(vi) And because "linha" has been botched up, the value of "fun" is incorrect too.
You probably wanted the value of "fun" to be:
- "Jose Antonio Borges" while processing line 1.
- "Silvia Maria" while processing line 2.
Have a look at Scrutinizer's script. It reads a line, breaks it up into appropriate tokens (while reading itself) and then compares the 2nd token with the name that you input.
Hope that clears up your doubts.
tyler_durden