Introduction to Bash Scripting for SQL Server DBAs
You are a SQL Server Database Administrator starting to work with Linux to run SQL Server. Everything is fine until you realize that there are some tasks like handling backups that are executed inside a Bash script and you have no idea how to deal with Bash scripts. In this tip I will show you the basics of Bash Scripting.
A script is a succession of instructions that are intended to be executed automatically instead of executing each instruction by hand. One of the very distinctive characteristics of scripts is that they are not compiled like executable programs. Instead scripts are interpreted by an interpreter when they are run.
Most of us are used to working with Windows, examples of scripts on Windows are .bat and .cmd files which are interpreted by the command line (cmd.exe), .ps1 files which are interpreted by the PowerShell shell and .vbs files which are interpreted by the Visual Basic Script cscript.exe interpreter.
Bash (/bin/bash) is the default shell for Linux. There are other shells available like /bin/sh, but you may not have all of the shells in your Linux system. In such case you must install the desired shell by using the packet manager of your Linux distribution.
A bash script is basically a text file. Some people name bash scripts as *.sh but it is not mandatory. You can just create a bash script named "myscript" and it will have the same effect if it named it "myscript.sh". On Linux file extensions don’t have the same meaning like on Windows. I mean an executable program on Windows must have the .exe or .com extension, but Linux handles this with permissions.
No matter the name or content of a program or script file, in order to be able to execute it you must assign the execution permission to the script file. I will show you an example of it further on.
Structure of a Script in Linux
On Linux all script files have the same structure regardless of its interpreter. Take a look:
#![Interpreter] #Comments Commands
The first line of a shell script is the one that instructs Linux about the interpreter to use in order to parse and execute the script. Usually it is a shell like /bin/bash, but also you can specify for example a Python interpreter. Take a look at these examples:
This example is the header of a script that will be interpreted by bash shell.
This example is the header of a script that will be interpreted by the Python interpreter.
Besides the first line, anything followed by a hash sign (#) is considered a comment. For commands, I mean every valid command supported by the interpreter which in this tip we will assume it is /bin/bash. In other words, you can use all the commands you use in bash shell like ls, mkdir, rm just to mention a few of them. But also, you can invoke executable files, either binaries or scripts. Everything that you can run in bash can be a command of a bash script.
Components of a Bash Script
Bash scripting is not limited to just writing a set of commands into a text file. It has all the features of any procedural language that we need to use in order to write more complex scripts that you can see in the list below.
- Declaration of Variables and Constants
- Display User Messages and Catch User Input
- Conditional Statements (If then, etc.)
- Iterative Statements (for, while and until loops)
- Redirections (Reading and Writing to Files)
- Regular Expressions
- Signal Catching
Setting Execution Permission to a Script
On Linux the only way to make a script or program to be executed is by assigning the execution permission. This permission can be assigned separately to the owner, to the owner’s group and to other users by using the chmod command as follows.
chmod [Octal Permissions] [File]
I found it easier to deal with the octal representation than with the rwx (read write execute) representation. Each of those permissions (rwx) has a numeric equivalent that you will see on the next table:
When you use the octal notation to represent the permissions you just have to sum the values of the previous table.
|Effective Permission||Permission Sum||Value|
|Read + Write||4 + 2||6|
|Read + Execute||4 + 1||5|
|Read + Write + Execute||4 + 2 + 1||7|
Suppose you have a script file sample.sh and you want to assign
- Read + Write + Execute (7) permission to the owner of the file
- Read + Execute (5) to the owner’s group
- and Read (4) for everyone else.
You should write the following command:
chmod 754 sample.sh
Creating the Hello World Bash Script
Now let’s create our first bash script with the classic "Hello World!" message. The first step is to create a new file sample.sh. I will be using nano text editor, but you can use any you want.
On the editor of your choice, copy and paste the following lines and save the file.
#!/bin/bash echo "Hello World!"
The echo command writes in the console whatever you pass as a parameter, in this case "Hello World!".
After saving the contents of that file we have to grant execution permission so we can run the script. On the next code I am setting read, write and execute permission for all users of the system.
chmod 777 sample.sh
Now to run the script, since it is located in the current directory, I have to use the following syntax to execute it.
The dot "." On the previous code refers to the current directory.
On the previous screen capture you can see the output from the execution of the previous script.
- Stay tuned for the next tips on this series.
- Are you new on SQL Server running on Linux? You must read this tip: Getting Started with SQL Server on Linux.
- Are you a SQL Server DBA new to Linux? Here you will find 7 Things Every SQL Server DBA Should Know About Linux.
- In this tip you will find the Top 10 Linux Commands for SQL Server DBAs.
- Take a look at SQL Server on Linux Tips Category.
Last Updated: 2018-11-06
About the author
View all my tips