How to change database script files from SH format to SQL format under Linux

Source: Internet
Author: User
Tags chmod create index

1. sh File contents

The file name in this article is example.sh, which reads as follows:

#!/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 NOT NULL--employee work number

EmployeeName varchar NOT null--employee name

employeeage int null--Employee age

);

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

Description

(1) This file is used to create the Tb_employeeinfo table, and the generated script file name is Example.sql.

(2) The INIT function is used to output information on the screen, and the CreateTable function is used to create data tables.

(3) At the end of the sh file, list all functions contained in this document in order, such as the functions included in this document are Init and createtable.

2. Process of generating SQL files

(1) Upload sh file

Use the FTP tool (such as FileZilla) to upload the example.sh file to the corresponding directory on Linux.

(2) using the Dos2unix command to modify the file format

Because the example.sh file is written under a local Windows operating system, it must be converted to a Linux format before it can be used. If you use it directly after uploading, the error message of "Permissiondenied" will appear.

The Dos2unix command is used to convert a DOS-formatted text file into UNIX format. The format used is: Dos2unix file, if you convert multiple files at once, follow these file names directly after Dos2unix (Dos2unixfile1 file2 file3 ...).

Here, the command is executed as follows:

Zhou@linux:~/sql> Dos2unix example.sh

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

(3) Permission to modify files using the chmod command

After the Dos2unix command is executed, you cannot immediately generate the file, and you need to modify the permissions for the file.

The chmod command is one of the most commonly used commands in a Linux system to change the access rights of a file or directory. For more information about this command, please check online.

Here, the order is: chmod 777 example.sh

(4) Generate SQL files

You can generate SQL files by running the SH file name with the suffix directly. The order is as follows:

Zhou@linux:~/sql> example.sh

Example.sql no exits and is creating it

Indicates that the Example.sql file does not exist before, this is the first generation.

To execute the command again:

Zhou@linux:~/sql> example.sh

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

Indicates that the Example.sql file already exists and is now deleted and regenerated.

3. sql file contents

The generated SQL file name is example.sql and the file contents are as follows:

Use zxdbp_166

Go

CREATE TABLE Tb_employeeinfo

(

Employeeno varchar NOT NULL--employee work number

EmployeeName varchar NOT null--employee name

employeeage int null--Employee age

);

Create unique index Idx1_tb_employeeinfo ontb_employeeinfo (Employeeno);

CREATE INDEX Idx2_tb_employeeinfo ontb_employeeinfo (employeename);

print ' CREATE table tb_employeeinfo OK '

Go

Cross-platform operations are common in real software development projects. As a qualified software Development engineer, we must be proficient in operating procedures and commands under different operation systems.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.