Overview
Just like other programming languages, the shell supports the use of functions, which leads to more structured and easier to maintain scripts
The here document is used to output multiple lines of text from the script. The here document is not a function, but it is used to group lines of text into a mini text file within a script, making it easier to work with multiple lines of text.
Topics:
Functions
Here Document
Often in programming, a particular logical block of code needs to run at several places in the script
For example, a logical block of code keeps looping to get a filename and until it gets a valid file
This block of code could need to run at several places in the script, whenever a file is needed from the user
Rather than copy and paste the block of code over and over again in the script, we create a function from the block of code and put it at the beginning of the script
Then, every time a file name is needed, we only need to call the function, we don’t need to repeat the block of code in the script
Functions are useful because they:
Reduce the size of the script (less typing by the programmer)
Can be re-used by many scripts (no need to reinvent the wheel)
Produce logically structured (organized) scripts that are easy to read, debug, and maintain
The shell is an interpreter, not a compiler, so it interprets the script line by line from top to bottom of the file
This means that the shell needs to “see” the function block before the function is called. Otherwise, the shell will give a syntax error if it sees the function call and has not seen the function block
In a script, all function blocks are put at the beginning of the script, then the main block of code appears last in the script
The order in the script file will be:
1. all function blocks
2. the main script commands
Syntax for a Function
The function block is made of 2 parts: function header and function body
functionA ()
{
commands
...
commands
optional return value
}
The header has the function name, followed by empty parentheses
It is recommended that the function name be descriptive so it’s easy to recognize what task the function does
The empty parentheses are required, and they are always empty. Without the parentheses the shell will interpret the function name as a variable name
The function body starts and ends with { } , and in between are commands and programming constructs for the function to do its task
It is recommended that the commands in a function body be indented from the function header so it’s easy to read
The commands and programming constructs should be left justified (line up on the left) and further indented if necessary
If a function is short, the entire function can be coded on the same line. In this case, every command in the function body must end with semicolon
Example: goHome ( ) { cd; echo At `pwd`; }
When a function is called, we can give input arguments to the function the same way that we give input arguments on the command line:
functionA file1 file2
functionA is called and gets 2 filenames as input
The function accesses its input through the positional parameters $1, $2, $3, etc.
Example of input arguments, given the function call above
functionA ( )
{
if [[ -e $1 && -e $2 ]]
then
echo both files exist
else
echo at least one filename is not valid
fi
}
A function can return one and only one integer value, through the return statement:
return $size
The value in variable size is returned to the caller
The integer return value is in the range of 0 - 255
The return value is stored in the ? variable and is accessed by the caller with $?
Example
functionB ( )
{
size=‘ls –l $1 | awk {print $5}` # get file size
return $size
}
call functionB and pass in fileA, then access return value to print:
functionB fileA
echo fileA has $? bytes