3

I'm having a little problem getting variables from a csv file on my Linux machine and using them in an if. I have the following csv:

Name;Age
Marc;18
Joseph;10

I'm trying to get this information from my csv to use in a function:

My bash code :

#!/bin/ksh

while IFS=";" read -r c_1 c_2

do echo "Name : $c_1" echo "Age : $c_2"

if [ $c_2 == "18" ] then echo "$c_1 can drive" fi

done < <(tail -n +2 teste_input.csv)

After I run my code, I get :

Name : Marc
Age : 18
Name : Joseph
Age : 10

I was expecting:

Name : Marc
Age : 18
Marc can drive
Name : Joseph
Age : 10

I already tried a lot of stuff without success. I can use c_1 and c_2 as input to some functions with no problem. The problem is just when I try to do math operations.

If I do:

s=$c_2+1
d=$((${c_2}+1))

it returns :

+1
+1")syntax error: invalid arithmetic operator (error token is "

Same problem using d=$(("${c_2}+1")).

What should I change to make it run? Using #!/bin/ksh, #!/bin/sh or #!/bin/bash gives me the same results.

Thanks for your help :)

  • this is not "bash code". it's a ksh script, as is evident from the #!/bin/ksh line. – cas Jun 02 '21 at 09:14
  • There is a lot of different between ksh, bh and bash? I have the same problem using which one of them. – Marcos Vinícius Gomes Jun 02 '21 at 09:18
  • Yes, there are differences (especially between sh and the other two) but that's not the issue here. Also, the shebang (#!/bin/ksh) is only half the story. How do you run the script? Do you call it directly (path/to/script.sh) or do you call it with a specific shell (e.g. sh /path/to/script.sh)? – terdon Jun 02 '21 at 09:23
  • Also, in the script you are running, echo '$c_1 can drive' is probably echo "$c_1 can drive" (double quotes), otherwise it would print $c_1 can drive (literally). Is this correct? – fra-san Jun 02 '21 at 09:30
  • That said, you likely have Windows-style newline sequences (CR+LF) in your input file. See https://unix.stackexchange.com/q/79702/315749 and https://unix.stackexchange.com/q/32001/315749. – fra-san Jun 02 '21 at 09:35
  • @terdon i call as 'bash script.sh' inside the directorie. Its give me ' path/ bash script.sh' – Marcos Vinícius Gomes Jun 02 '21 at 09:50
  • @fra-san you right, its "$c_1 can drive". – Marcos Vinícius Gomes Jun 02 '21 at 09:51
  • OK, if you run it as bash script.sh, then the shebang (the first line, e.g. #!/bin/ksh) is irrelevant: the script will be run by bash. The shebang only works if you just run the script directly (./script.sh). – terdon Jun 02 '21 at 09:55

1 Answers1

6

Your first problem is that you are using a Windows text file. Your csv has Windows style line endings (\r\n). You can fix that using:

dos2unix teste_input.csv

Or if you don't have dos2unix installed, just do:

sed -i 's/\r//g' teste_input.csv

That will make your code produce the output you are expecting. Almost:

$ foo.sh teste_input.fixed.csv 
Name : Marc
Age : 18
$c_1 can drive
Name : Joseph
Age : 10

The $c_1 there is because of the single quotes in this line:

echo '$c_1 can drive'

Variables are not expanded inside single quotes so that's why you see $c_1 and not the variable's value. You need:

echo "$c_1 can drive"

The next issue is that == is for string comparison and not arithmetic. It will work here, but if you want to compare numerical values, you want -eq:

if [ $c_2 -eq "18" ]

However, based on your "can drive" output, I suspect you want to check if the variable's value is equal to or greater than 18, so you would use -ge:

 if [ $c_2 -ge "18" ]

Finally, you need to pick one shell to use. ksh is not the same as bash which is not the same as sh. While they have extremely similar syntax, it isn't identical and some things will work differently depending on what you use. For instance, sh doesn't support <(command).

As for the arithmetic you tried, you just need to get the syntax right, you need to use an "arithmetic context". s=$c_2+1 is just doing a simple variable assignment. It is setting the value of the variable $s to the the value of $c_2 with the string +1 added:

$ c_2=10
$ s=$c_2+1
$ echo "$s"
10+1

Your second example, d=$((${c_2}+1)) does work, because (( )) sets an arithmetic context:

$ c_2=10
$ d=$((${c_2}+1))
$ echo "$d"
11

You got the syntax error because of the Windows line endings:

$ c_2=10$'\r'
$ d=$((${c_2}+1))
+1")syntax error: invalid arithmetic operator (error token is "

So that will go away if you fix the file as I described in the beginning.

terdon
  • 242,166