Introduction to Bash Scripting for SQL Server DBAs

By:   |   Comments   |   Related: > SQL Server on Linux


Problem

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.

Solution

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 Scripting

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.

#!/bin/bash

This example is the header of a script that will be interpreted by the Python interpreter.

#!/usr/bin/env python

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)
  • Functions
  • 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:

Permission Number
Read 4
Write 2
Execute 1

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
Execute 1 1
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.

nano sample.sh

On the editor of your choice, copy and paste the following lines and save the file.

#!/bin/bash
echo "Hello World!"
Creating our "Hello World!" script.

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.

./sample.sh

The dot "." On the previous code refers to the current directory.

This is the output from the execution of the previous script.

On the previous screen capture you can see the output from the execution of the previous script.

Next Steps


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Daniel Farina Daniel Farina was born in Buenos Aires, Argentina. Self-educated, since childhood he showed a passion for learning.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips



Comments For This Article

















get free sql tips
agree to terms