0

I'm trying to put together a script to bulk-update a bunch of database tables. I have a list in a file (but I could just as well have it from stdin), and I'm trying to construct the ALTER TABLE commands. I have the following

while read table; do echo "ALTER TABLE $table ENGINE=Aria"; done < tables

which however outputs something like this

 ENGINE=Ariamydatabase.mytableone
 ENGINE=Ariamydatabase.mytabletwo
 ENGINE=Ariamydatabase.mytablethree

instead of

ALTER TABLE mydatabase.mytableone ENGINE=Aria;
...

like I expected. What am I doing wrong? Eventually, I'd change echo to mysql -uroot -ppassword -e"ALTER TABLE $table ENGINE=Aria;".

Morpheu5
  • 2,610
  • 6
  • 39
  • 72

2 Answers2

0

The file tables has DOS \r\n line endings. This causes $table to end with \r carriage returns, which when printed move the cursor back to the beginning of the line.

You'll want to convert tables to UNIX \n line endings.

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
-2

Cat command will handle with your new line saperators.

Try this code:

$ cat tables | while read table; do echo "ALTER TABLE $table ENGINE=Aria"; done
Ali Okan Yüksel
  • 338
  • 1
  • 13