In the last chapter, you learned about using decision making in scripts by working with test, if-else, and switch case. We also used select for loop with menu. For repeated tasks, such as processing lists, you learned to use the for and while loops and the do while. You also learned about how to control loops using the break and continue statements.
In this chapter, you will learn the following topics:
We, human beings, in our day-to day lives, take help from people, who are specialized in certain knowledge or skills, such as doctors, lawyers, and barbers. This helps our lives to be more organized and comfortable so that we need not learn every skill in this world. We take advantage of skills that have already been acquired by other people. The same thing applies to software development as well. If we use whatever code or scripts that have already been developed, then this will save our time and energy.
In real-world scripts, we break down big tasks or scripts into smaller logical tasks. This modularization of scripts helps in the better development and understanding of code. The smaller logical blocks of script are be called functions.
The advantages of functions are as follows:
Functions can be defined on a command line or inside scripts. The syntax for defining functions on a command line is as follows:
functionName { command_1; command_2; . . . }
Or:
functionName() { command_1; command_2; . . }
In single-line functions, every command should end with a semicolon.
Let's write a very simple function to illustrate the preceding syntax:
$ hello() {echo 'Hello world!';}
We can use the previously defined function as follows:
$ hello
Output:
Hello world!
The syntax of the function declaration inside the Shell script is as follows:
function_name() {
block of code
}An alternate function syntax is mentioned here:
function function_name
{
block of code
}Functions should be defined at the beginning of a script.
We can add this function in the Shell script function_01.sh as follows:
#!/bin/bash
hello()
{echo "Executing function hello"
}
echo "Script has started now"
hello
echo "Script will end"Test the script as follows:
$ chmod +x function_01.sh $ ./function_01.sh
Output:
Script has started now Executing function hello Script will end
We can modify the preceding script into function_02.sh with some more functionality, shown as follows:
#!/bin/bash
function greet()
{ echo "Hello $LOGNAME, today is $(date)"; }
greetTest the script as follows:
$ chmod +x function_02.sh $ ./function_02.sh
Output:
Hello ganesh, today is Sun Jul 5 22:47:23 PDT 2015
The system init functions are placed in the /lib/lsb/init-functions folder in the Linux operating system:
The script function_03.sh with a function for listing the present working directory and listing all the files in the current directory is as follows:
#!/bin/bash
function_lister ()
{
echo Your present working directory is `pwd`
echo Your files are:
ls
}
function_listerTest the script as follows:
$ chmod +x function_03.sh $ ./function_03.sh
Output:
Your present working directory is /home/student/Desktop/test Your files are: 01.sh 02.sh 03.sh
The script function_04.sh with a function to pause the script until users press any key is as follows:
#!/bin/bash
# pause: causes a script to take a break
pause()
{
echo "To continue, hit RETURN."
read q
}
pauseTest the script as follows:
$ chmod +x function_04.sh $ ./function_04.sh
Output:
To continue, hit RETURN. (after hitting any key it resumes)
The script function_05.sh with a function to print the previous day is as follows:
#!/bin/bash
yesterday()
{
date --date='1 day ago'
}
yesterdayTest the script as follows:
$ chmod +x function_05.sh $ ./function_05.sh
Output:
Sat Jul 4 22:52:24 PDT 2015
The function to convert lowercase letters into uppercase letters is shown in function_06.sh as follows:
#!/bin/bash
function Convert_Upper()
{
echo $1 | tr 'abcdefghijklmnopqrstuvwxyz' \
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
}
Convert_Upper "ganesh naik - embedded android and linux training"$ chmod +x function_06.sh $ ./function_06.sh
Output:
GANESH NAIK - EMBEDDED ANDROID AND LINUX TRAINING