In Linux, how does one change the database script file from the sh format to the SQL format,
In the process of software development, it often involves changing the database script file from sh format to SQL format in Linux. This document uses an actual script file as an example to describe the format conversion process.
1. sh File Content
The file name in this article is example. sh, and its content is 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 (20) not null, -- employee ID
Employeename varchar (20) 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
Note:
(1) This file is used to create the tb_employeeinfo table. 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 a data table.
(3) At the end of the sh file, list all functions contained in this file in sequence. For example, the functions included in this file are Init and CreateTable.
2. SQL file generation process
(1) Upload the sh File
Use the FTP tool (such as filezilla) to upload the example. sh file to the corresponding directory of Linux.
(2) Use the dos2unix command to modify the file format
Because the example. sh file is written in a local Windows operating system, you must first convert it to a Linux format. If it is used directly after upload, the error message "Permissiondenied" is displayed.
The dos2unix command is used to convert a text file in DOS format to a UNIX format. The format is dos2unix file. If multiple files are converted at a time, the file names are directly followed by dos2unix (dos2unixfile1 file2 file3 ...).
Run the following command:
Zhou @ linux :~ /SQL> dos2unix example. sh
Dos2unix: converting file example. sh to UNIX format...
(3) Use the chmod command to modify File Permissions
After the dos2unix command is executed, the file cannot be generated immediately, and the file permission must be modified.
The chmod command is one of the most commonly used commands in Linux and is used to change the access permissions of files or directories. For more information about this command, go to the Internet.
Here, the command is: chmod 777 example. sh
(4) generate an SQL File
Run the sh file name with a suffix to generate an SQL file. The command is as follows:
Zhou @ linux :~ /SQL> example. sh
Example. SQL no exits and is creating it
It indicates that the example. SQL file does not exist before. This is the first generation.
Run the following command again:
Zhou @ linux :~ /SQL> example. sh
Example. SQL is exits and is deleting it, then recreate it
It indicates that the example. SQL file already exists. delete the file and regenerate it.
3. SQL File Content
The generated SQL file name is example. SQL. The file content is as follows:
Use zxdbp_166
Go
Create table tb_employeeinfo
(
Employeeno varchar (20) not null, -- employee ID
Employeename varchar (20) 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 actual software development projects. As a qualified software development engineer, you must be familiar with the operating procedures and commands in different operating systems.
(My microblogging: http://weibo.com/zhouzxi? Topnav = 1 & wvr = 5, No.: 245924426, welcome !)