Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Vaardigheden in dit hoofdstuk
- Beslissingen nemen met if, test, [[ ]] en (( ))
Write an if statement
if runs a command. If the command succeeds (exit status 0), the then part runs; otherwise the else part.
Understand the exit status
Every command ends with an exit status from 0 to 255. 0 means success; any other number means an error. $? holds the last status.
Test files
test expression or [ expression ] checks something and returns 0 (true) or 1 (false). Spaces around [ and ] are required.
| Expression | True if the file … |
|---|---|
-e file | exists |
-f file | exists and is a regular file |
-d file | exists and is a directory |
-L file | is a symbolic link |
-r / -w / -x file | is readable / writable / executable for you |
-s file | exists and is not empty |
-O file | is owned by you |
file1 -nt file2 | is newer than file2 |
file1 -ot file2 | is older than file2 |
file1 -ef file2 | is the same file (hard link) |
#!/bin/bash
# test-file: evaluate the status of a file
FILE=~/.bashrc
if [ -e "$FILE" ]; then
if [ -f "$FILE" ]; then
echo "$FILE is a regular file."
fi
if [ -d "$FILE" ]; then
echo "$FILE is a directory."
fi
if [ -r "$FILE" ]; then
echo "$FILE is readable."
fi
else
echo "$FILE does not exist"
exit 1
fi
exitOnthoud
Put variables in quotes inside [ ]: [ -e "$FILE" ]. An empty variable without quotes gives a syntax error.
Test strings
String tests check if a text is empty or equal to another text.
| Expression | True if |
|---|---|
string / -n string | the string is not empty |
-z string | the string is empty |
s1 = s2 (or ==) | the strings are equal |
s1 != s2 | the strings are different |
s1 > s2 / s1 < s2 | s1 sorts after / before s2 (escape in [ ]: \>) |
#!/bin/bash
ANSWER=maybe
if [ -z "$ANSWER" ]; then
echo "There is no answer." >&2
exit 1
fi
if [ "$ANSWER" = "yes" ]; then
echo "The answer is YES."
elif [ "$ANSWER" = "no" ]; then
echo "The answer is NO."
elif [ "$ANSWER" = "maybe" ]; then
echo "The answer is MAYBE."
else
echo "The answer is UNKNOWN."
fi>&2sends the error message to stderr.
Test integers
Numbers are compared with letter operators, not with < and >.
| Expression | True if |
|---|---|
a -eq b | equal |
a -ne b | not equal |
a -le b / a -lt b | less than or equal / less than |
a -ge b / a -gt b | greater than or equal / greater than |
#!/bin/bash
INT=-5
if [ -z "$INT" ]; then
echo "INT is empty." >&2
exit 1
fi
if [ "$INT" -eq 0 ]; then
echo "INT is zero."
else
if [ "$INT" -lt 0 ]; then
echo "INT is negative."
else
echo "INT is positive."
fi
if [ $((INT % 2)) -eq 0 ]; then
echo "INT is even."
else
echo "INT is odd."
fi
fiUse the modern [[ ]]
[[ ]] is bash's better test. It adds regex matching with =~ and pattern matching with ==, and it does not split words.
Use (( )) for numbers
(( )) tests arithmetic. It is true when the result is not zero. Variables need no $, and <, >, == work normally.
if ((INT == 0)); then
echo "INT is zero."
else
if ((INT < 0)); then
echo "INT is negative."
else
echo "INT is positive."
fi
if (( ((INT % 2)) == 0)); then
echo "INT is even."
else
echo "INT is odd."
fi
fiCombine conditions
AND, OR and NOT join tests. The signs depend on the test form.
MIN_VAL=1
MAX_VAL=100
INT=50
if [[ "$INT" -ge "$MIN_VAL" && "$INT" -le "$MAX_VAL" ]]; then
echo "$INT is within $MIN_VAL to $MAX_VAL."
fi
if [ ! \( "$INT" -ge "$MIN_VAL" -a "$INT" -le "$MAX_VAL" \) ]; then
echo "$INT is outside $MIN_VAL to $MAX_VAL."
fiBranch with && and ||
cmd1 && cmd2 runs cmd2 only if cmd1 succeeds. cmd1 || cmd2 runs cmd2 only if cmd1 fails.
Finish the report: check for root
Use if in the report script: only root may read all home directories.
report_home_space () {
if [[ "$(id -u)" -eq 0 ]]; then
cat <<- _EOF_
<h2>Home Space Utilization (All Users)</h2>
<pre>$(du -sh /home/*)</pre>
_EOF_
else
cat <<- _EOF_
<h2>Home Space Utilization ($USER)</h2>
<pre>$(du -sh "$HOME")</pre>
_EOF_
fi
return
}Summary of the chapter
if checks an exit status; [ ], [[ ]], (( )) make tests; && and || are short branches.
1Which exit status means success?
0.
2Which test checks that a variable is a whole number?
3What does [[ -d backup ]] || mkdir backup do?
It makes the directory backup only if it does not exist yet.