Writing high-quality scripts by example
In this recipe, we are going to see functions in shell scripts. We will see how our program testing is done, sequentially on the various parts, using functions. Functions help improve the readability of a program.
Getting ready
Besides having a terminal open, you need to have a basic knowledge of functions.
How to do it...
We are going to write a simple function in our shell script to return the current date and time. Create a script function_example.sh
, and write this code in it:
#!/bin/bash print_date() { echo "Today is `date`" return } print_date
Now we will create another script containing two functions with the same name. Create a script function2.sh
, and write the following content in it.
#!/bin/bash display ( ) { echo 'First Block' echo 'Number 1' } display ( ) { echo 'Second Block' echo 'Number 2' } display exit 0
How it works...
In the first script, we created a function named print_date()
and we just printed a date using a function.
In the second...