当前位置: 技术问答>linux和unix
shell脚本执行流程为什么不对?
来源: 互联网 发布时间:2016-11-24
本文导语: 初学shell, 这个脚本通过命令行输入1个参数(-mysql/-informix), 然后完成各种数据库的创建和初始化过程。 但是输入参数无论是mysql还是informix,只执行的是一个数据库。 流程肯定有问题。 请各位高手指点!! 以下...
初学shell,
这个脚本通过命令行输入1个参数(-mysql/-informix),
然后完成各种数据库的创建和初始化过程。
但是输入参数无论是mysql还是informix,只执行的是一个数据库。
流程肯定有问题。
请各位高手指点!!
以下是我的代码逻辑:
这个脚本通过命令行输入1个参数(-mysql/-informix),
然后完成各种数据库的创建和初始化过程。
但是输入参数无论是mysql还是informix,只执行的是一个数据库。
流程肯定有问题。
请各位高手指点!!
以下是我的代码逻辑:
#!/bin/sh
####read DBTYPE and DB parameters#####
CALLER=`basename $0`
DBTYPE=$1
# DB=$2
DB="securitydb"
TABLESCRIPT="securitytables.sql"
STATICSCRIPT="loadstaticdata.sql"
echo "the first parameter is:" $1
echo "the second parameter is:" $2
usage()
{
echo "in usage"
}
# #########use case statement############
error_infxdir()
{
echo "ERROR!!!!!! INFORMIXDIR environment variable INFORMIXDIR not set."
exit 1
}
error_infxserver()
{
echo "ERROR!!!!!! INFORMIXSERVER environment variable INFORMIXSERVER not set."
exit 1
}
error_createspace()
{
echo " ************************************************************************"
echo "****ERROR!!!!!! create %DB% dbspace failed,you should re-create dbspace.****"
echo "************************************************************************"
exit 1
}
success()
{
echo "********************************"
echo "***** The $DB database created success! ******"
echo "********************************"
exit 0
}
############### informix handle begin #####################################################################
informixdb()
{
echo "in informix:"
echo "you must confirm informix service started and enviorment varable has been set first!"
SECONDDRV=d:
DBSPACE=securitydbspace
DBSPACEFILE=securitydbs
DBSPACESIZE=100000
SHELLDIR=
echo "INFORMIXDIR: " $INFORMIXDIR
echo "INFORMIXSERVER: " $INFORMIXSERVER
# If exsit $DB database
echo "Drop the exsit $DB database..."
###execute SQL statement use dbaccess??
echo "DROP DATABASE $DB" | dbaccess -
# echo "drop dbspace $DBSPACE..."
####execute onspaces directly
onspaces -d $DBSPACE -y
# create directory for dbspace
mkdir $INFORMIXDIR/dbs
# cd $INFORMIXDIR/dbs
# pwd
touch $INFORMIXDIR/dbs/$DBSPACEFILE
chmod 660 $INFORMIXDIR/dbs/*
###linux/solaris dbspace location
onspaces -c -d $DBSPACE -p $INFORMIXDIR/dbs/$DBSPACEFILE -o 0 -s $DBSPACESIZE
pwd
dbaccess - - !
cd ./informix/tablescript
echo "current path:"
pwd
if [ -f "$TABLESCRIPT" ]; then
echo "$TABLESCRIPT founded! delete file!"
rm -f $TABLESCRIPT
fi
echo "create the table script..."
cat *.sql>$TABLESCRIPT
echo "execute the script $TABLESCRIPT..."
dbaccess $DB $TABLESCRIPT
cd ./../../
echo "current path:"
pwd
echo "load static data..."
cd ./informix/staticdata
dbaccess $DB $STATICSCRIPT
cd ./../../
success
}
############### informix handl"$1"="-informix"e end #####################################################################
############### mysql handle begin #####################################################################
mysqldb()
{
echo "in mysql:"
echo "you must confirm mysql service started and enviorment varable has been set first!"
# ## DB="securitydb"
cd ./mysql/tablescript
echo "current path:"
pwd
if [ -f "$TABLESCRIPT" ]; then
echo "$TABLESCRIPT founded! delete file!"
rm $TABLESCRIPT
fi
echo "create the table script..."echo "Sorry, $YES_OR_NO not recognized. Enter yes or no."
## !!!solaris(liuguiqiu):error message!!error dropping database(can't ...)errno=17
echo "DROP DATABASE IF EXISTS $DB;">>$TABLESCRIPT
echo "CREATE DATABASE IF NOT EXISTS $DB;">>$TABLESCRIPT
echo "USE $DB;">>$TABLESCRIPT
## !!!linux: message!!cat: securitytables.sql: input file is output file
cat *.sql>>$TABLESCRIPT
echo "execute the script $TABLESCRIPT..."
mysql -f -u root -p