0

I am trying to create a bash script that will populate my mysql table Players in a database called linus from a csv file called Players. The csv file has 10 columns named:

 teams, ranking, games, wins, draws, losses, goalsFor, goalsAgainst, yellowCards, redCards

table has the same 10 columns with the same names. I am getting a strange syntax error referring to the last line in my script:

  "syntax error near unexpected token `done'"

I cannot seem to see what the issue is with my code.

#!/bin/bash
input="Players.csv"
while IFS= read -r var
do
    team='echo $var | cut ,'
    ranking='echo $var | cut ,'
    games='echo $var | cut ,'
    wins='echo $var | cut ,'
    draws='echo $var | cut ,'
    losses='echo $var | cut ,'
    goalsFor='echo $var | cut ,'
    goalsAgainst='echo $var | cut ,'
    yellowCards='echo $var | cut ,'
    redCards='echo $var | cut ,'
    mysql -h localhost -u linus --password= mypassword -D
         linus -e \insert into Players "($team,$ranking,$games,$wins,$draws,$losses,$goalsFor,$goalsAgainst,$yellowCards,$redCards)"
    done < "$input"
Cathal Brady
  • 117
  • 1
  • 3
  • 10
  • 1
    (1) Run your code through http://shellcheck.net. (2) This is running `linus` as a command, not passing it as an argument to `mysql`. – Charles Duffy Mar 21 '20 at 22:41
  • 1
    (3) `echo $var | cut ,` doesn't do what you want it do. Use `while IFS=, read -r team ranking games wins draws losses goalsFor goalsAgainst yellowCards redCards; do ...` to let `read` itself separate your content into variables. – Charles Duffy Mar 21 '20 at 22:42
  • 1
    (4) Don't use bash to read CSV files at all. It doesn't do a good job of it, and there are mysql-native tools that do better. See in particular `LOAD DATA`, which you can use to tell mysql itself to handle all the parsing. – Charles Duffy Mar 21 '20 at 22:43
  • 2
    (5) I wouldn't be surprised if your file was saved as a Windows text file instead of a UNIX one. The difference between those is that Windows text files have an invisible `$'\r'` character right before the newline. Bash sees `do` as a valid keyword, but doesn't recognize `$'do\r'` that way. – Charles Duffy Mar 21 '20 at 22:45
  • 1
    (6) `lines -e \insert into Players` is passing only the word `insert` as an argument to `-e`; it isn't passing the entire following text as a single argument, which is what `mysql -e` expects. – Charles Duffy Mar 21 '20 at 22:47
  • Thanks Charles for the constructive feedback I am new to Bash and have to use it in this example but I will try use your comments to make a workable solution. – Cathal Brady Mar 21 '20 at 23:02
  • Some of the answers to the linked duplicates (up at the top of the question) should be usable pretty much as-is. See f/e https://stackoverflow.com/a/42933107/14122 – Charles Duffy Mar 21 '20 at 23:34
  • (btw, just to explain *why* I suspect that `do` is being misinterpreted as `$'do\r'` on account of DOS newlines -- that would explain why the `done` is unexpected, as a `done` following a `while` is expected only *after* a `do` block). – Charles Duffy Mar 22 '20 at 00:11

0 Answers0