Codementor Events

Writing Your First Linux Bash Script [Easy!]

Published Jul 28, 2023
Writing Your First Linux Bash Script [Easy!]

Introduction

Today let's create a basic bash script that we can run on our linux computers to do task automation.

Planning The Bash Script

Let's create a simple bash script that will display information about our linux server.

The Bash Script will gather the following information:

  • Current Disk usage
  • Current CPU usage
  • Current RAM usage
  • Check the exact Kernel version

Note: You can of course make your scripts do whatever you want, but this is just the example that we'll use today.

Writing the script

The first thing that you need to do is to create a new file with a .sh extension.

I will create a file called system-resource-monitor-bash.sh as the script that we will create would give us the status of our server.

Use the following command to create the bash script file.

touch system-resource-monitor-bash.sh 

Add This Line To The Top Of The File

Once you've created the file, open it with your favorite text editor.

We add this line to the top of the file to tell the operating to run the script with the /bin/bash executable.

#!/bin/bash

Adding comments

Let's add some comments so that people could easily figure out what the script is used for.

To do that right after the shebang you can just add the following:


#!/bin/bash

##
## BASH script to monitor system information
##
## This bash script performs to following checks:
##
##     * Memory Usage
##     * CPU Load
##
##     * TCP Connections
##     * Kernel Version
##

Creating Our First Variable In The Bash Script

Let's add variables that we will use across the entire script.

To assign a value to a variable in bash, you use the = sign.

For example, this is how to store the hostname of our server into a variable that we can use later:

local_hostname=$(hostname)

By using $() we tell bash to interpret the command and then assign the return value of the command back into the "local_hostname" variable.

If we echo out the local_hostname variable we see the linux server hostname:

echo $local_hostname

Adding your first function

Functions in Bash typically use the following structure:

function function_name() {
    your_commands
}

So lets create a function that returns the current memory usage on our server:


function monitor_system_memory_usage() {
    
    echo
    echo "##"
  echo "## Memory Usage // ${local_hostname}: "
    echo "##"
    echo
    
  free -h

  echo
  
}

Understanding How This Bash Function Works

  • function monitor_system_memory_usage() {

    this is how we define the function

  • echo ""

    here we just print a new line

  • echo "## Memory Usage // ${local_hostname}: "

    here we print a small message and the $local_hostname variable

  • }

    finally this is how we close the function

Calling Our Newly Created Function

Once the function has been defined, now we can call the function to use it.

So call our function in the code, we just use the name of the function.


function monitor_system_memory_usage() {
    
    echo
    echo "##"
  echo "## Memory Usage // ${local_hostname}: "
    echo "##"
    echo
    
  free -h

  echo
  
}

## Call the memory usage function

monitor_system_memory_usage

Taking This Script To The Next Level

Printing the memory usage is just stage 1.

So le's add more stuff on top of the system memory.

Now we'll update our bash script to do the following:

  • Current Disk usage
  • Current CPU usage
  • Check the exact Kernel version

The Complete System Monitor Script

Here's what the complete end result of the bash script will look like:


#!/bin/bash

##
## BASH script to monitor system information
##
## This bash script performs to following checks:
##
##     * Memory Usage
##     * CPU Load
##
##     * TCP Connections
##     * Kernel Version
##

local_hostname=$(hostname)

function monitor_system_memory_usage() {
    
    echo
    echo "##"
  echo "## Memory Usage // ${local_hostname}: "
    echo "##"
    echo
    
  free -h

  echo
  
}


function monitor_system_information() {
        
    echo
    echo "##"
  echo "## CPU Load // ${local_hostname}: "
    echo "##"
    echo
    
  uptime

  echo

}

function monitor_tcp_connections() {

    echo
    echo "##"
  echo "## TCP Connections // ${local_hostname}: "
    echo "##"
    echo
    
  cat  /proc/net/tcp | wc -l

  echo

}

function monitor_kernel_information() {

    echo
    echo "##"
  echo "## Kernel Version // ${local_hostname}: "
    echo "##"
    echo
    
  uname -r

  echo

}

function execute_all_system_monitoring() {
  
  monitor_system_memory_usage
  
  monitor_system_information
  
  monitor_tcp_connections
  
  monitor_kernel_information
  
}

execute_all_system_monitoring

Make The Bash Script Executable

Save the file and exit.

After that make the script executable by running the following command:

chmod +x system-resource-monitor-bash.sh

Verify Permissions Of Your Bash File

We'll need to verify the permissions on the bash file before we run it.

To do that, we simply run the 'ls -l' command:

ls -lha system-resource-monitor-bash.sh 

You want to see something similar to the following:

-rwxrwxr-x 1 software-shinobi software-shinobi 1,1K jun 24 10:47 system-resource-monitor-bash.sh

You are looking for there to be three "x" in the beginning part that looks like this:

-rwxrwxr-x

Executing The Bash File

So we've created the bash script and we've set the proper permissions.

Now let's run the bash script.

./system-resource-monitor-bash.sh

Or you can alternately use this:

bash system-resource-monitor-bash.sh

Or this:

source system-resource-monitor-bash.sh

Or this:

. system-resource-monitor-bash.sh

There are lots of ways to kick off a bash script on a linux server.

Conclusion

Bash scripts are great for automating tasks and activites on linux and unix systems.

Creating a bash script is rather quick and easy.

Today we created a simple bash script to monitor system resources information on our linux server.

Give these bash script tips a try on your linux server and be sure to tune in next time for the next linux tips!t writing here...

Discover and read more posts from Software Shinobi
get started
post commentsBe the first to share your opinion
sourabglansa
5 months ago

“Efficient resource management becomes an art form in Linux automation, where scripts intelligently allocate and deallocate resources on demand.”

https://amritahyd.org/automation-with-ansible/

Show more replies