Linux下如何将数据库脚本文件从sh格式变为sql格式

本文发布时间: 2019-Mar-22
在从事软件开发的过程中,经常会涉及到在Linux下将数据库脚本文件从sh格式变为sql格式的问题。本文以一个实际的脚本文件为例,说明格式转换的过程。1. sh文件内容本文中的文件名为example.sh,其内容如下:#!/bin/bashfunction Init(){if [ -f"example.sql" ]thenecho"example.sql is exits and is deleting it,then recreate it"rm -fexample.sqlelseecho"example.sql no exits and is creating it"fiecho " usezxdbp_166 ">>example.sqlecho " go">>example.sql}function CreateTable(){cat>>example.sql<< EOFcreate 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'goEOF}## Execute functionInitCreateTable说明:(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.shdos2unix: converting file example.sh to UNIX format ...(3) 使用chmod命令修改文件的权限在执行了dos2unix命令之后,还是不能立马生成文件,还需要修改文件的权限。chmod命令是Linux系统中最常用到的命令之一,用于改变文件或目录的访问权限。若想了解有关该命令的更多信息,请上网查询。在这里,命令为:chmod 777 example.sh(4) 生成sql文件直接运行带后缀的sh文件名,即可生成sql文件。命令如下:zhou@linux:~/sql> example.shexample.sql no exits and is creating it表示example.sql文件之前不存在,这是第一次生成。再次执行命令:zhou@linux:~/sql> example.shexample.sql is exits and is deleting it,then recreate it表示example.sql文件已经存在了,现在删除后重新生成。3. sql文件内容生成的sql文件名为example.sql,文件内容如下:use zxdbp_166gocreate 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在实际的软件开发项目中,跨平台操作是常有的事情。作为一名合格的软件开发工程师,一定要熟练掌握不同操作系统下的操作流程及命令。


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:10pm
栏目列表