Introduction to Bash Scripting

In Computer programming, a script is a set of commands for an appropriate run time environment that is used to automate the execution of tasks. A Bash Shell Script is a plain text file containing a set of various commands that we usually type in the command line. It is used to automate repetitive tasks on Linux filesystem.


A character sequence known as the "shebang" appears on the first line of Bash scripts. When the program loader executes the file, the shebang is the first instruction, and the characters indicate which interpreter to use when reading the script.


To indicate the use of the Bash interpreter, add the following line to the file:

#!/bin/bash

  • #! directs the program loader to load an interpreter for the code in the file.

  • /bin/bash the Bash interpreter's location.

if for example, we would like to use PowerShell interpreter, we will use "#!/usr/bin/pwsh".


Adding Comments

Lines that do not execute are referred to as comments. They do, however, aid in code readability. Add a comment after the shebang to explain what the script is.

H!/bin/bash
#This is a sample comment
#This is another comment

Adding Code

Bash scripts are used to make our work more efficient by automating manual tasks. Let's write and run our first bash script.

H!/bin/bash
#This is a sample comment
#This is another comment

echo "This is our first script"

echo "The uptime of the system is:"
uptime

echo "Disk utilization is"
df -h

echo "Memory utilization"
free -m



Run a script

in order to run a script, you will have to specify its absolute or relative path. Prior to running a script. you must ensure that the script has executable permission. Lets see an example:

[root@scriptbox scripts]# ./firstscript.sh
-bash: ./firstscript.sh: Permission denied
[root@scriptbox scripts]# ls -l
total 4
-rw-r--r--. 1 root root 213 Jun 19 10:41 firstscript.sh

As you can see, there is executable permission that is missing, adding and run the script:

[root@scriptbox scripts]# chmod +x firstscript.sh 

[root@scriptbox scripts]# ls -l
total 4
-rwxr-xr-x. 1 root root 213 Jun 19 10:41 firstscript.sh

Now we can run it:

[root@scriptbox scripts]# ./firstscript.sh
./firstscript.sh: line 1: H!/bin/bash: No such file or directory
This is our first script
The uptime of the system is:
 10:54:04 up 52 min,  1 user,  load average: 0.00, 0.01, 0.05
Disk utilization is
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 485M     0  485M   0% /dev
tmpfs                    496M     0  496M   0% /dev/shm
tmpfs                    496M  6.7M  489M   2% /run
tmpfs                    496M     0  496M   0% /sys/fs/cgroup
/dev/mapper/centos-root   50G  1.7G   49G   4% /
/dev/sda1               1014M  167M  848M  17% /boot
/dev/mapper/centos-home   28G   33M   28G   1% /home
vagrant                  477G  148G  330G  31% /vagrant
tmpfs                    100M     0  100M   0% /run/user/1000
Memory utilization
              total        used        free      shared  buff/cache   available
Mem:            990         110         451           6         429         736
Swap:          1023           0        1023

Let's change the code to make it more readable:

H!/bin/bash

#This is a sample comment
#This is another comment

echo "This is our first script"
echo
echo "-------------------------------"

echo "The uptime of the system is:"
uptime
echo

echo "-------------------------------"
echo "Disk utilization is"
df -h
echo


echo "-------------------------------"
echo "Memory utilization"
free -m

and let's run it:

[root@scriptbox scripts]# ./firstscript.sh
./firstscript.sh: line 1: H!/bin/bash: No such file or directory
This is our first script

-------------------------------
The uptime of the system is:
 11:01:39 up 59 min,  1 user,  load average: 0.00, 0.01, 0.05

-------------------------------
Disk utilization is
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 485M     0  485M   0% /dev
tmpfs                    496M     0  496M   0% /dev/shm
tmpfs                    496M  6.8M  489M   2% /run
tmpfs                    496M     0  496M   0% /sys/fs/cgroup
/dev/mapper/centos-root   50G  1.7G   49G   4% /
/dev/sda1               1014M  167M  848M  17% /boot
/dev/mapper/centos-home   28G   33M   28G   1% /home
vagrant                  477G  148G  330G  31% /vagrant
tmpfs                    100M     0  100M   0% /run/user/1000
tmpfs                    100M     0  100M   0% /run/user/0

-------------------------------
Memory utilization
              total        used        free      shared  buff/cache   available
Mem:            990         110         450           6         429         736
Swap:          1023           0        1023


Automating Website Setup using bash script

in this example, I will use a bash script to deploy a website on a centos machine.


First, let's create the bash file under the script folder and open it using VIM editor

[root@scriptbox scripts]# vim websetup.sh

Script Details:

#!/bin/bash

# Installing Dependencies
echo "########################################"
echo "Installing packages."
echo "########################################"
sudo yum install wget unzip httpd -y > /dev/null
echo

# Start & Enable Service
echo "########################################"
echo "Start & Enable HTTPD Service"
echo "########################################"
sudo systemctl start httpd
sudo systemctl enable httpd
echo

# Creating Temp Directory
echo "########################################"
echo "Starting Artifact Deployment"
echo "########################################"
mkdir -p /tmp/webfiles
cd /tmp/webfiles
echo

wget https://www.tooplate.com/zip-templates/2098_health.zip > /dev/null
unzip 2098_health.zip > /dev/null
sudo cp -r 2098_health/* /var/www/html/
echo

# Bounce Service
echo "########################################"
echo "Restarting HTTPD service"
echo "########################################"
systemctl restart httpd
echo

# Clean Up
echo "########################################"
echo "Removing Temporary Files"
echo "########################################"
rm -rf /tmp/webfiles
echo

sudo systemctl status httpd
ls /var/www/html/

lets test it by using the machine IP http://192.168.10.12/:




6 views0 comments