How to write a shell script to create a MySQL database

A shell script is a computer program designed to be run by the Unix shell, a command line interpreter. Shell scripts can help developers to help automate repetitive tasks so that we don't have write long commands again and again.

In this post, we will go through the following topics:

  1. What is a shell script?
  2. Anatomy of a shell script
  3. Writing a simple shell script
  4. Making the script executable
  5. Writing a script to create a new database

1. What is a shell script?

Shell script is a series of commands written in a file with extension .sh . Shell scripts are read and executed by the bash program. It can be a list of some commands that we need to run repetitively.

2. Anatomy of a shell script

Lets look at the following example:

#!/bin/bash

# These are comments

echo “Please enter your name: “
read name
echo -e “Hello $name!”

Let's go through the example line by line:

#!/bin/bash This top line defines the interpreter to be used by the shell script.

#Comments Comments are not executed as part of the shell script and are used for documentation.

echo “Please enter your name: “ This line outputs a text in the screen.

read name This line takes a variable as an input from the command line.

echo -e “Hello $name!\n” This line prints the variable on the screen.

3. Writing a simple shell script

Let's open the terminal and create a file name hello_world.sh with the following command:

$ touch hello_world.sh

Open the newly created file using the following command (We will use vim, but any text editor is fine)

$ vim hello_world.sh

Then, type the following commands in the file hello_world.sh

#!/bin/bash

# These are comments

echo “Please enter your name: “
read name
echo -e “Hello $name!”

Quit the vim editor using the following command:

$ :wq

Shell script blog post 2.png

Shell script blog post _1.png

4. Making the script executable

We need to make the file executable to run the shell script, for this we need to run the following command:

$ chmod u+x hello_world.sh

3.png

Notice the file name color has changed from the previous screenshot.

Now we can the run the script by using the following command:

$ hello_world.sh

Output:

4.png

5. Writing a simple to script to create a database in MySQL

Create a file called create_db.sh and file executable using following commands:

$ touch create_db.sh
$ chmod u+x create_db.sh

Open the file using the command vim create_db.sh and type the following commands in the newly created file.

#!/bin/bash
echo "Please enter the root user password: "
read -s rootpassword
echo "Please enter the name of the new MySQL database"
read databasename
echo "Creating new MySQL database .... "
mysql -uroot -p${rootpassword} -e "CREATE DATABASE ${databasename} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
echo "Database successfully created .... "
echo "Good bye!"
exit

Let's run the script file to create a database:

$ ./create_db.sh

After following the command line and giving the root password and database name, a new database is created in MySQL.

7.png

6.png

That's it, we have just created a database using the shell script.

Thanks for reading!