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 25Naam in het boek: Starting a Project

Start a project: an HTML report script

From here on, the chapters build one program: sys_info_page, a script that writes a report about the system as an HTML page. Step by step it gets variables, functions, tests, loops and options. This chapter makes the first version and introduces variables, constants and here documents.

  • 6 Lessen
  • 7 Commando's
  • 1 Labs

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

25.1

Make a minimal document

An HTML page is plain text. The first version prints a fixed page with echo.

~/bin/sys_info_page (version 1)
#!/bin/bash
 
# Program to output a system information page
 
echo "<html>
  <head>
    <title>Page Title</title>
  </head>
  <body>
    Page body.
  </body>
</html>"
chmod 755 ~/bin/sys_info_page
sys_info_page > sys_info_page.html
firefox sys_info_page.html
  • One echo with a quoted string can span many lines. That is shorter than one echo per line.
  • Redirect the output to a .html file and open it in a browser.

25.2

Add data with variables

The title appears two times. A variable stores it once; change it in one place.

~/bin/sys_info_page (version 2)
#!/bin/bash
 
# Program to output a system information page
 
TITLE="System Information Report For $HOSTNAME"
CURRENT_TIME="$(date +"%x %r %Z")"
TIMESTAMP="Generated $CURRENT_TIME, by $USER"
 
echo "<html>
  <head>
    <title>$TITLE</title>
  </head>
  <body>
    <h1>$TITLE</h1>
    <p>$TIMESTAMP</p>
  </body>
</html>"
  • $HOSTNAME and $USER are shell variables that already exist.
  • $(date …) is command substitution: the output of date goes into the variable.

Commando's in deze les

25.3

Know the rules for variables and constants

A variable is made the first time you give it a value. Names have rules. Constants are variables that do not change; by habit they are written in CAPITALS.

  • A name contains letters, digits and _. It must not start with a digit. No spaces, no punctuation.
  • A misspelled name is not an error: the shell makes a new, empty variable. Watch your typing.
  • Habit: CAPITALS for constants and environment variables, lower case for normal variables.
  • declare -r TITLE="…" makes a real constant: changing it later is an error.
foo="yes"
echo $foo
yes
echo $fool
 
declare -r TITLE="Report"
TITLE="Other"
bash: TITLE: readonly variable

Commando's in deze les

25.4

Assign values to variables

name=value with no spaces around =. The value can come from text, variables, command substitution and arithmetic.

examples of assignments
a=z                   # the string "z"
b="a string"          # spaces need quotes
c="a string and $b"   # other variables are expanded
d="$(ls -l foo.txt)"  # the output of a command
e=$((5 * 7))          # arithmetic
f="\t\ta string\n"    # escape codes (shown with echo -e)
a=5 b="a string"      # many on one line
filename="myfile"
touch "$filename"
mv "$filename" "$filename1"
mv: missing destination file operand after 'myfile'
mv "$filename" "${filename}1"

Onthoud

Use braces when text follows the name: ${filename}1. Without them, bash looks for a variable called filename1.

25.5

Use here documents

A here document sends many lines of text to a command's stdin. The text ends at a token you choose, like _EOF_.

~/bin/sys_info_page (version 3)
#!/bin/bash
 
# Program to output a system information page
 
TITLE="System Information Report For $HOSTNAME"
CURRENT_TIME="$(date +"%x %r %Z")"
TIMESTAMP="Generated $CURRENT_TIME, by $USER"
 
cat << _EOF_
<html>
  <head>
    <title>$TITLE</title>
  </head>
  <body>
    <h1>$TITLE</h1>
    <p>$TIMESTAMP</p>
  </body>
</html>
_EOF_
  • Inside a here document, single and double quotes are normal characters. Variables and $( ) are still expanded.
  • Quote the token (<< '_EOF_') to stop all expansion.
  • <<- (with a dash) removes leading tabs, so you can indent the text in the script.
  • The end token must be alone on its line, at the start (or after tabs with <<-).
a here document gives commands to ftp
ftp -n << _EOF_
open $FTP_SERVER
user anonymous me@linuxbox
cd $FTP_PATH
get $REMOTE_FILE
bye
_EOF_

Op het werk

Here documents are perfect for config files and emails written by a script: cat > /etc/app.conf << EOF.

Commando's in deze les

25.6

Summary of the chapter

Build a script in small working steps. Variables avoid repeated text; here documents hold blocks of text.

1What is wrong with title = "Report"?

There are spaces around =. bash then tries to run a command called title. Write title="Report".

2How do you write the value of file followed by .bak?

"${file}.bak".

3How do you stop variables from expanding inside a here document?

Quote the token: cat << '_EOF_'.

Oefenen

Alle commando's in dit hoofdstuk