Geavanceerd
Zoeken inSamenvattingen
Zo zoek je
  • Woorden vinden lestitels, tekst en code. Filter op soort (gidsen, cursussen, programma's, boeken) of blijf in dit boek.
  • book:rhcsa en ch:8 maken de zoektocht kleiner; #code vindt een woord alleen in commando's van de lessen.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Hoofdstuk 26Naam in het boek: Top-Down Design

Top-down design with functions

Big problems are easier when you split them into small steps, and each step into smaller steps. This is top-down design. In a script, each step becomes a shell function. This chapter adds functions to the report script, explains local variables, and shows how to keep a script running while you build it.

  • 7 Lessen
  • 4 Commando's
  • 1 Labs

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

26.1

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.

26.2

Write shell functions

A function is a named block of commands. It must be defined before it is called. Two forms exist.

the two forms
function name {
    commands
    return
}
 
name () {
    commands
    return
}
a small script with a function
#!/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.
  • return ends the function; return n gives an exit status (see Make decisions with if).
  • A function must contain at least one command. return is enough.

Commando's in deze les

26.3

Use local variables

Normal variables are global: the whole script sees them. local makes a variable that only exists inside the function.

local_vars
#!/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 = 0

Onthoud

Use local for all variables inside functions. Then functions cannot break each other's data.

26.4

Keep the script running: stubs

Add empty functions (stubs) first. The script runs at every step, and you see that the structure works.

~/bin/sys_info_page (with stubs)
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.

26.5

Fill in the functions

Replace each stub with the real work. HTML <pre> keeps the command output as it is.

the real functions
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.

Commando's in deze les

26.6

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.

in ~/.bashrc
ds () {
    echo "Disk Space Utilization For $HOSTNAME"
    df -h
}
 
mkcd () {
    mkdir -p "$1" && cd "$1"
}
source ~/.bashrc
ds
mkcd ~/projects/new
type ds

26.7

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.

Oefenen

Alle commando's in dit hoofdstuk