Today I thought I would write a small bash script to create DB tables on a SQL database. What I thought would be quick turned out differently. Anyway I'm at a mental block and need advice. What I was want to do in the end is have the user enter the amount of columns in the table, then specify the column label and data type for the amount they entered to begin with. Then I'll take the array elements to assemble my SQL server command. It seemed rather obvious to me at first but not so much anymore. Any help is appreciated!
I was going to do something like...
#!/bin/bash
echo "Enter database name: "
read dbname
echo "Enter table name: "
read tablename
echo "Enter column count: "
read columns
echo "Enter database username: "
read dbuser
echo "Enter database user password: "
read dbpw
for i in ${columns[@]} do
echo "Column "$i" Name: "
read array_cname[$i]
echo "Column "$i" Data Type: "
read array_ctype[$i]
done
table="USE $dbname CREATE TABLE $tablename(array_cname[] array_ctype[] etc etc etc)"
mysql -u$dbuser -p$dbpw -e "$table"
The above is obviously a broken script, I just whipped it up after having cannibalized what I originally had going.
CREATE TABLE (and before). What out for SQL injections.