1

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.

3
  • At this point I've pretty much destroyed my code lol. Let me see if I can whip up the original quickly. Commented Mar 31, 2016 at 16:22
  • Updated, hope this helps! Commented Mar 31, 2016 at 16:40
  • You need to do another loop do concatenate each column definition after CREATE TABLE ( and before ). What out for SQL injections. Commented Mar 31, 2016 at 17:00

1 Answer 1

1

I'm just going to look at the part that you're struggling with - looping over the columns - and you should be able to take these lessons back to your script.

This is what I wrote, omitting the easy bits you've done, and setting columns to 2, to save typing whilst I tested.

#!/bin/bash

columns=2

declare -a cols

for ((i=1; i<=$columns; i++))
do
    read -p "Column $i Name: " n
    read -p "Column $i Data Type: " t
    cols[$i]="$n $t"
done

echo "CREATE TABLE t (${cols[*]} etc etc etc)"

Explanation

  • I use declare to ensure that our array variable is of the correct type. Bash will infer it's an array when we first assign to an element, but I like to be clear.
  • To enumerate the integers from 1 to $columns, I use a Bash for loop.
  • You were missing the semicolon or newline between for and do.
  • Instead of trying to read directly into array elements, I read into two temporaries: n for name and t for type. I also use -p to make read emit the prompt for us.
  • I don't use two separate arrays for name and type, because Bash doesn't provide a simple way to interleave the values. Instead, I put name and type into a single array element. I could put them into separate elements if needed, with cols+=("$n" "$t").
  • To expand all elements of the array in a substitution, I use ${cols[*]}.
  • I haven't done any validation on the input; I'm assuming this script is a convenience for your own use, rather than a robust tool to share.

Test result

$ ./36337995.sh
Column 1 Name: a
Column 1 Data Type: int
Column 2 Name: b
Column 2 Data Type: int
CREATE TABLE t (a int b int etc etc etc)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.