Linux在什么样的从脚本文件数据库sh格式改变sql格式

       在软件开发过程中,经常参与Linux从下一个脚本文件数据库sh格式改变sql格式问题。在本文中,一个实际的脚本文件,例如。描述格式转换过程。

       1. sh文件内容

       本文中的文件名称为example.sh,其内容例如以下:

#!/bin/bash

 

function Init()

{

    if [ -f"example.sql" ]

    then

        echo"example.sql is exits and is deleting it,then recreate it"

        rm -fexample.sql

    else

        echo"example.sql no exits and is creating it"

    fi

    echo " usezxdbp_166 ">>example.sql

    echo " go">>example.sql

}

 

function CreateTable()

{

cat>>example.sql<< EOF

 

create table tb_employeeinfo

(

    employeeno        varchar(20)      not null,      -- 员工工号

    employeename    varchar(20)      not null,      -- 员工姓名

    employeeage       int                          null       -- 员工年龄

);

create unique index idx1_tb_employeeinfo ontb_employeeinfo(employeeno);

create index idx2_tb_employeeinfo ontb_employeeinfo(employeename);

      

print 'create table tb_employeeinfo ok'

go

 

 

EOF

}

 

## Execute function

Init

CreateTable

 

        说明:

        (1)  本文件用于创建tb_employeeinfo表。生成的脚本文件名称为example.sql。

        (2)  Init函数用于在屏幕上输出信息,CreateTable函数用于创建数据表。

        (3)  在sh文件的结尾。要按顺序将本文件所包括的全部函数罗列出来,如本文件包括的函数是Init和CreateTable。

 

        2. 生成sql文件的过程

        (1)  上传sh文件

        使用FTP工具(如filezilla)将example.sh文件上传到Linux的相应文件夹下。

 

        (2)  使用dos2unix命令改动文件格式

       因为example.sh文件是在本地的Windows操作系统下编写的。因此要先转换为Linux下的格式才干使用。

假设上传后直接使用,会出现“Permissiondenied”的报错信息。

       dos2unix命令用来将DOS格式的文本文件转换成UNIX格式的。其使用的格式为:dos2unix file,假设一次转换多个文件,把这些文件名称直接跟在dos2unix之后(dos2unixfile1 file2 file3 …)。

       在这里,命令运行例如以下:

zhou@linux:~/sql> dos2unix example.sh

dos2unix: converting file example.sh to UNIX format ...

 

        (3)  使用chmod命令改动文件的权限

        在运行了dos2unix命令之后,还是不能立刻生成文件,还须要改动文件的权限。

        chmod命令是Linux系统中最经常使用到的命令之中的一个,用于改变文件或文件夹的訪问权限。若想了解有关该命令的很多其它信息。请上网查询。

        在这里,命令为:chmod 777 example.sh

 

        (4)  生成sql文件

       直接运行带后缀的sh文件名称,就可以生成sql文件。

命令例如以下:

zhou@linux:~/sql> example.sh

example.sql no exits and is creating it

       表示example.sql文件之前不存在。这是第一次生成。

 

       再次运行命令:

zhou@linux:~/sql> example.sh

example.sql is exits and is deleting it,then recreate it

      表示example.sql文件已经存在了,如今删除后又一次生成。

 

        3. sql文件内容

        生成的sql文件名称为example.sql。文件内容例如以下:

use zxdbp_166

go

 

create table tb_employeeinfo

(

    employeeno         varchar(20)      not null,      -- 员工工号

    employeename    varchar(20)      not null,      -- 员工姓名

    employeeage       int                           null       -- 员工年龄

);

create unique index idx1_tb_employeeinfo ontb_employeeinfo(employeeno);

create index idx2_tb_employeeinfo ontb_employeeinfo(employeename);

      

print 'create table tb_employeeinfo ok'

go

 

        在实际的软件开发项目中。跨平台操作是常有的事情。作为一名合格的软件开发project师,一定要熟练掌握不同操作系统下的操作流程及命令。



(本人微博:http://weibo.com/zhouzxi?topnav=1&wvr=5,我们聊天号码:245924426。欢迎关注!

)


版权声明:本文博客原创文章。博客,未经同意,不得转载。