Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Vaardigheden in dit hoofdstuk
- Een programma opdelen in functies met lokale variabelen
Think top-down
Write the big steps first, then fill in the details. "Go to the market" becomes: get in the car, drive, park, walk in …
The report needs these parts:
- the uptime and load of the system,
- the disk space,
- the space used by home directories.
Each part becomes a function: report_uptime, report_disk_space, report_home_space.
Write shell functions
A function is a named block of commands. It must be defined before it is called. Two forms exist.
function name {
commands
return
}
name () {
commands
return
}#!/bin/bash
# Shell function demo
function step2 {
echo "Step 2"
return
}
# Main program starts here
echo "Step 1"
step2
echo "Step 3"- When the shell reaches
step2, it runs the function and then continues after the call. returnends the function;return ngives an exit status (see Make decisions with if).- A function must contain at least one command.
returnis enough.
Use local variables
Normal variables are global: the whole script sees them. local makes a variable that only exists inside the function.
#!/bin/bash
# local-vars: script to demonstrate local variables
foo=0 # global variable foo
funct_1 () {
local foo # variable foo local to funct_1
foo=1
echo "funct_1: foo = $foo"
}
funct_2 () {
local foo # variable foo local to funct_2
foo=2
echo "funct_2: foo = $foo"
}
echo "global: foo = $foo"
funct_1
echo "global: foo = $foo"
funct_2
echo "global: foo = $foo"local-vars
global: foo = 0
funct_1: foo = 1
global: foo = 0
funct_2: foo = 2
global: foo = 0Onthoud
Use local for all variables inside functions. Then functions cannot break each other's data.
Keep the script running: stubs
Add empty functions (stubs) first. The script runs at every step, and you see that the structure works.
report_uptime () {
echo "Function report_uptime executed."
return
}
report_disk_space () {
echo "Function report_disk_space executed."
return
}
report_home_space () {
echo "Function report_home_space executed."
return
}
cat << _EOF_
<html>
<head>
<title>$TITLE</title>
</head>
<body>
<h1>$TITLE</h1>
<p>$TIMESTAMP</p>
$(report_uptime)
$(report_disk_space)
$(report_home_space)
</body>
</html>
_EOF_$(report_uptime)inside the here document puts the output of the function into the page.
Fill in the functions
Replace each stub with the real work. HTML <pre> keeps the command output as it is.
report_uptime () {
cat <<- _EOF_
<h2>System Uptime</h2>
<pre>$(uptime)</pre>
_EOF_
return
}
report_disk_space () {
cat <<- _EOF_
<h2>Filesystem Space Utilization</h2>
<pre>$(df -h)</pre>
_EOF_
return
}
report_home_space () {
cat <<- _EOF_
<h2>Home Directory Space Utilization</h2>
<pre>$(du -sh /home/*)</pre>
_EOF_
return
}Notitie
du -sh /home/* needs root to read other users' home directories. The next chapter checks this with if.
Put functions in .bashrc
Functions can also replace aliases. A function in ~/.bashrc is there in every shell and can take arguments and use logic.
Summary of the chapter
Split the problem top-down, one function per step, local variables, stubs first, then the real code.
1Why must a function be defined before it is called?
bash reads the script from top to bottom; at the call the function must already be known.
2What does local do?
It makes a variable that exists only inside the function, so it does not change a global variable with the same name.
3What is a stub?
An empty function that only shows it was called; it lets the script run while the real code is not written yet.