In the process of software development, it often involves the problem of changing the database script file from SH format to SQL format under Linux. This article takes an actual script file as an example to illustrate the process of format conversion.
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)--worker 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 is named Example.sql.
(2) The INIT function is used to output information on the screen, and the CreateTable function is used to create the data table.
(3) At the end of the sh file, all the functions contained in this file are listed sequentially, as the functions included in this file are init and createtable.
2. Procedure for generating SQL files
(1) Upload sh file
Use the FTP tool (such as FileZilla) to upload the example.sh file to the corresponding directory in Linux.
(2) using the Dos2unix command to modify the file format
Since the example.sh file is written under the local Windows operating system, it must be converted to a format under Linux before it can be used. If used directly after uploading, the "permissiondenied" error message will appear.
The Dos2unix command is used to convert a DOS-formatted text file into UNIX format. It is used in the format: Dos2unix file, if you convert more than one file at a time, put these file names directly after Dos2unix (Dos2unixfile1 file2 file3 ...).
Here, the command executes as follows:
[Email protected]:~/sql> Dos2unix example.sh
dos2unix:converting file example.sh to UNIX format ...
(3) using the chmod command to modify the permissions of a file
After executing the Dos2unix command, you still cannot generate the file immediately, and you need to modify the permissions of 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 command is: chmod 777 example.sh
(4) Generate SQL file
The SQL file can be generated by running the SH file name with the suffix directly. The command is as follows:
[Email protected]:~/sql> example.sh
Example.sql no exits and is creating it
Indicates that the Example.sql file does not exist before, which is the first build.
Execute the command again:
[Email protected]:~/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 regenerated after deletion.
3. sql file contents
The generated SQL file is named Example.sql, and the file contents are as follows:
Use zxdbp_166
Go
CREATE TABLE Tb_employeeinfo
(
Employeeno varchar (NOT NULL)--worker number
EmployeeName varchar (NOT NULL)--Employee name
employeeage int null-employee age
How to change a database script file from SH format to SQL format under Linux