You are not logged in.

#1 2007-07-18 16:02:06

FeatherMonkey
Member
Registered: 2007-02-26
Posts: 313

Critics welcome a little Bash Script

#!/bin/bash
new=`kdialog --inputbox "Whats the Name of the DataBase?"`
echo "CREATE DATABASE $new;" > new
echo "USE $new;" >> new
name="total"

function data {
multi=`kdialog --combobox "Select Data type;" "VARCHAR(100)" "INT" "TEXT" "BINARY" "VARBINARY" "TINYBLOB" "TINYTEXT" "BLOB" "MEDIUMBLOB" "MEDIUMTEXT" "LONGBLOB" "LONGTEXT"`
}

function create {
    dat=`kdialog --inputbox "Title of First Data Field for Table $name" "total"`
    data
    echo "CREATE TABLE $name (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, $dat $multi, cur_timestamp TIMESTAMP(8));" >> new
    add=`kdialog --radiolist "Select Number of Additional Fields For $name:" 0 "Zero" on 1 "One" off 2  "Two" off 3 "Three" off 4 "Four" off 5 "Five" off 6 "Six" off 7 "Seven" off`
}

function mod {
if [ $add -gt '0' ]
    then
    while [ $add -gt 0 ]
    do
    col=`kdialog --inputbox "Column Heading for Table $name;"`
    echo $col
    data
    echo "alter table $name add column $col $multi;" >> new
    add=`expr $add - 1`
    done
fi
echo "show columns FROM $name;" >> new
}

function extra {
mark=`kdialog --radiolist "Number additional Tables for $name:" 0 "Zero" on 1 "One" off 2  "Two" off 3 "Three" off 4 "Four" off 5 "Five" off 6 "Six" off 7 "Seven" off`
#echo $name
while [ $mark -gt 0 ]
do
name=`kdialog --inputbox "Extra Table Heading;"`
create
mod
mark=`expr $mark - 1`
title="|--| $name"
echo $title
mark2=`kdialog --radiolist "Number additional Tables for $name:" 0 "Zero" on 1 "One" off 2  "Two" off 3 "Three" off 4 "Four" off 5 "Five" off 6 "Six" off 7 "Seven" off`
    while [ $mark2 -gt 0 ]
    do
    name=`kdialog --inputbox "Extra Table Heading;"`
    create
    mod
    mark2=`expr $mark2 - 1`
    title="-|- $name"
    echo $title
    done
done
}

title="|**| $name"
echo $title
create

name="incoming"
create
mod
title="|**| $name"
echo $title
extra

name="outgoing"
create
mod
title="|**| $name"
echo $title
extra
ans=`kdialog --password "Enter Password:" "******"`
#echo $ans
mysql -u root -p $ans < new

As the title suggests, I'm learning bash a little, now this creates 3 base tables in a mysql database with an optional 2 deep, maximum 7 tables for 2 of the base tables(I'm also looking at a way to link them further on down the line) using KDE kdialog boxes,

My first one is I cant seem to pass the variable to the mysql command, is this possible?

Also I'm missing error trapping, was hoping for a little feed back how I should tackle this(This is an area I need to work on)?

Offline

#2 2007-07-18 17:50:49

harlekin
Member
From: Germany
Registered: 2006-07-13
Posts: 408

Re: Critics welcome a little Bash Script

I am not quite familar with dialog but stumbled over how this _can_ be done. I don't know where I found this example:

dialog --clear ---inputbox "Dialog" 10 60 2>foo.tmp

Then the input will be written in foo.tmp and can be read later. To me it seems like a work-around but maybe it's the proper way. Anyway, it is a suggestion. AIS, I am not familar with dialog.

edit: Sorry. Now I notice that you're talking about kdialog (which is even more unknown to me ^^). Well... maybe it's helping anyway.

Last edited by harlekin (2007-07-18 17:51:58)


Hail to the thief!

Offline

#3 2007-07-19 01:03:07

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Critics welcome a little Bash Script

A quick look:

echo "CREATE DATABASE $new;" > new
echo "USE $new;" >> new

this can shortened to :

echo -e "CREATE DATABASE $new;\nUSE $new" > new

also, its best to get out of the habit of using backtick execution. $() is the new prefered bash syntax and will be enforced sometime in the distant future.

Offline

#4 2007-07-19 10:26:00

FeatherMonkey
Member
Registered: 2007-02-26
Posts: 313

Re: Critics welcome a little Bash Script

Thanks for the input guys.

Hadn't realised the back tick was on its way out looks like I've got to change a few more things than I thought, easier now to break the habit rather than later. wink

ans=$(kdialog --password "Enter Password:" "******"); would this be the correct syntax then? I also guess using this will give me the variable $? which I presume I could then use for error trapping. Like this

if [ $? = '0' ]
then
else
fi

Would this be the correct way, though presumably the neater way would be to wrap it in a function?

Makes sense when you got rid of the second echo with a new line.

Have thought about using a tmp file to hold the passwd just didn't really want to do it though. Also not quite sure how I'd amend it to the mysql command. Though thinking about it I presume I could make that earlier and place it at the beginning of the file being read in(Presuming it would work).

Offline

Board footer

Powered by FluxBB