How To - Shell Scripting - Bash - Basics - 1 - Functions

How To - Shell Scripting - Bash - Basics - 1 - Functions

Hello Everyone,

I will try publishing a series of articles on shell scripting. We are going to use bash which is default shell in almost all famous linux & unit operating systems.

Functions

The theory behind creating a function is simple, if you have a piece of code and you are going to use over and over, then instead of rewriting the code, you will make it as a function.

Function Syntax:

function function_name {
# commands
}

In bash we have one more syntax for definiting functions.

function_name() { 
# commands
}

So lets get to the point, where functions are for better use. For example, lets say you are printing a format message for the commands you have executed and your code is like below

uptime_output=$(uptime)
free_ram=$(free -m)
cpu_core=$(nproc)

echo "Command : uptime output $uptime_output"
echo "Command : Free RAM $free_ram"
echo "Command : CPU Cores $cpu_core"

Here the line echo "Command : uptime output $uptime_output" has a pattern and its repeating for all the commands that we have executed. Now we got 3 commands, assume we have 10 commands, then without functions, we have to write this for 10 times.

Instead if we go with functions, our code looks more cleaner way and easy to manage in future.

function format_message {
  first_arg=$1
  shift # first arg is string, $2 onwards we have output, to set cursor to read from $2, we are using shift
  echo "Command $first_arg output: " "$@"
}
format_message "uptime: " "$(uptime)"
format_message "Free RAM: " "$(free -m)"
format_message "Number of CPU Cores: " "$(nproc)"

So now no matter, how many commands, you have you dont have to write that format message everytime.

But, wait a minute, this is still can be optimized, so all you are doing, doing something repetitively, we can use a for loop in shell scripting

cmds=('uptime' 'free -m' 'nproc') # Bash Array
function format_output {
  first_arg=$1
  shift
  echo -e "Execution output of $first_arg\n$@"
}

for cmd in "${cmds[@]}"; # iterating over bash array
do
format_output "$cmd" "$($cmd)"
done

and that’s it.

You only have to update the cmds variable with new command and your code behaves in the same way for every command.

vagrant@vagrant-ubuntu-trusty-64:~$ bash for_my_script.sh
Execution output of uptime
 05:04:49 up  1:18,  1 user,  load average: 0.06, 0.02, 0.00
Execution output of free -m
              total        used        free      shared  buff/cache   available
Mem:            488         106          43           1         338         357
Swap:             0           0           0
Execution output of nproc
1
vagrant@vagrant-ubuntu-trusty-64:~$

This post has covered

  • How to define functions in bash
  • How to pass command line arguments functions
  • How to manipulate command line arguments
  • How to define array and iterate over array in bash

Hope it helps.
Thank you.

0 comments:

Post a Comment