script to convert CSV to SQL

I am trying to write a script to convert csv files into SQL format.
What I am missing is:

  1. handling of the first row and create it as a insert into format
  2. better handling of any other row, and create a line with the data. *The while read line needs a better work.
    I thought of using awk.

I will appreciate any help.

This is what I have so far (taken from readfile2.sh script)

# Make sure we get file name as command line argument
# Else read it from standard input device
if [ "$1" == "" ]; then
   echo "No File - Exit"
   exit 1
else
   FILE="$1"
   # make sure file exist and readable
   if [ ! -f $FILE ]; then
        echo "$FILE : does not exists"
        exit 1
   elif [ ! -r $FILE ]; then
        echo "$FILE: can not read"
        exit 2
   fi
fi

# field seprator, default is :, you can use blank space or
# other character, if you have more than one blak space in
# input line then use awk utility and not the cut :)
FS=","

while read line
do
        # store field 1
        F1=$(echo $line|cut -d$FS -f1)
        # store field 2
        F2=$(echo $line|cut -d$FS -f6)
        # store field
        F3=$(echo $line|cut -d$FS -f7)
        echo "User \"$F1\" home directory is $F2 and login shell is $F3"
done < $FILE

If you're going to use a "while read ... " construct, why not specify enough variables to cover what you need?

eg.

while read f1 f2 f3 f4 f5 f6 f7 f8
do
  insert f1 f6 f7
done < $FILE

"insert" might be a function you create to do the actual sql insert. Specify an f8 variable so that if there are more columns than 7 you just put everything after the 7th column into f8 which you don't need.

This elimnates the need to echo $line | cut ...

Also use the special shell variable "IFS" and set that to "," before going into the while loop I've described. Do a man on your shell and search for "IFS" for more info on this.
You probably want to set a variable IFS_HOLD=$IFS before changing the IFS variable, so that you can restore it later in the script, should you need the default IFS behavior later on.