ch8 functions and libraries

functions allow to manage chunks of code and make your code modular
functions hide implementation details and allow consistent reuse of code
functions can be tested over and over again as a small piece of a larger script

The standard Bourne shell syntax
uses the function name followed immediately by a pair of parentheses () and curly brackets {}
around the code itself.

$ cat myfunction.sh
#!/bin/bash
myfunction()
{
    echo “This is the myfunction function.”
}
# Main code starts here
echo “Calling myfunction...”
myfunction
echo “Done.”

$ ./myfunction.sh
Calling myfunction...
This is the myfunction function.
Done.
$

----------
There is a second syntax, which is not accepted by the Bourne shell, although bash and ksh both
accept it. Instead of following the function name by a pair of parentheses, the function name is preceded by the keyword function:
function myfunction
----------
Yet another syntax, accepted by bash alone, is to combine both elements.
function myfunction()
----------
Because the Bourne syntax is accepted by all shells, that is the most common one in use
myfunction()
{
}
----------

$ cat square-cube.sh
#!/bin/bash
squarecube()
{
echo “$2 * $2” | bc > $1
echo “$2 * $2 * $2” | bc >> $1
}
output=`mktemp`
for i in 1 2 3 4 5
do
squarecube $output $i
square=`head -1 $output`
cube=`tail -1 $output`
echo “The square of $i is $square”
echo “The cube of $i is $cube”
done
rm -f $output
$ ./square-cube.sh
The square of 1 is 1
The cube of 1 is 1
The square of 2 is 4
The cube of 2 is 8
The square of 3 is 9
The cube of 3 is 27
The square of 4 is 16
The cube of 4 is 64
The square of 5 is 25
The cube of 5 is 125
$

----------------
A recursive function is one that calls itself as part of its own execution.

$ cat factorial.sh
#!/bin/bash
factorial()
{
if [ “$1” -gt “1” ]; then
previous=`expr $1 - 1`
parent=`factorial $previous`
result=`expr $1 \* $parent`
echo $result
else
echo 1
fi
}
factorial $1
$ ./factorial.sh 6
720

----------
When your shell is invoked interactively, it reads ~/.profile, and (depending on the exact shell in use) ~/.bashrc, ~/.kshrc, or similar. Using the same method, your script can invoke (source) other files to gain their contents, without running any actual code.
---------
Creating a library is the same as creating a shell script, except that there is no actual starting point —
all you do with a library is define functions
------------
Libraries do not have to be marked as executable, and do not normally have any extension at all. Libraries also should not start with the #!/ syntax,
as they will not be executed by the operating system, but only read in by the shell itself.

To include a library in a shell script, the . or source command is called with the name of the library
file

The source command reads everything in the file into the current environment so any function definitions in the library will be taken on by the currently executing shell. Similarly, any variables defined there will be set in the current shell.
-------------

Comments

Popular posts from this blog

HAproxy logging

teamcity Automatic Agent Start under Linux

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all