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"
linusas a command, not passing it as an argument tomysql.echo $var | cut ,doesn't do what you want it do. Usewhile IFS=, read -r team ranking games wins draws losses goalsFor goalsAgainst yellowCards redCards; do ...to letreaditself separate your content into variables.LOAD DATA, which you can use to tell mysql itself to handle all the parsing.$'\r'character right before the newline. Bash seesdoas a valid keyword, but doesn't recognize$'do\r'that way.lines -e \insert into Playersis passing only the wordinsertas an argument to-e; it isn't passing the entire following text as a single argument, which is whatmysql -eexpects.