Linux下如何將資料庫指令碼檔案從sh格式變為sql格式,

來源:互聯網
上載者:User

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

 

        在實際的軟體開發項目中,跨平台操作是常有的事情。作為一名合格的軟體開發工程師,一定要熟練掌握不同作業系統下的操作流程及命令。



(本人微博:http://weibo.com/zhouzxi?topnav=1&wvr=5,號:245924426,歡迎關注!)





相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.