Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Make a minimal document
An HTML page is plain text. The first version prints a fixed page with echo.
#!/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.htmlAdd data with variables
The title appears two times. A variable stores it once; change it in one place.
#!/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>"$HOSTNAMEand$USERare shell variables that already exist.$(date …)is command substitution: the output ofdategoes into the variable.
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.
Assign values to variables
name=value with no spaces around =. The value can come from text, variables, command substitution and arithmetic.
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 linefilename="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.
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/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
<<-).
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.
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_'.