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 :)
kshscript, as is evident from the#!/bin/kshline.shand 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)?echo '$c_1 can drive'is probablyecho "$c_1 can drive"(double quotes), otherwise it would print$c_1 can drive(literally). Is this correct?