Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Vaardigheden in dit hoofdstuk
- Een script schrijven, uitvoeren en plaatsen zodat het werkt als een commando
Know what a shell script is
The shell is both a command interpreter and a programming language: variables, loops, decisions, functions. A script uses all of this.
- Scripts automate work: backups, reports, checks, installs.
- Part 4 of the book builds one bigger project step by step: a script that writes a system information report as an HTML page.
Write the script file
A script needs three things: a text file with the commands, execute permission, and a place in the PATH.
#!/bin/bash
# This is our first script.
echo 'Hello World!' # This is a comment too- Line 1 is the shebang (
#!): it tells the kernel which interpreter runs the file. Always put it first. - Everything after
#is a comment and is ignored.
Let op
Write scripts in a text editor (vim, nano, VS Code), never in a word processor. Windows editors may add CR characters that break the shebang ("bad interpreter").
Make it executable
The file needs the x permission. 755 lets everyone run it; 700 only you.
Put it in the PATH
hello_world alone fails, because the current directory is not in PATH. Put your scripts in ~/bin (or ~/.local/bin), which most distributions add to PATH.
hello_world
bash: hello_world: command not found
echo $PATH
/home/me/.local/bin:/home/me/bin:/usr/local/bin:/usr/bin:/bin
mkdir -p ~/bin
mv hello_world ~/bin
hello_world
Hello World!
export PATH=~/bin:"$PATH"| Place | For |
|---|---|
~/bin or ~/.local/bin | Your own scripts |
/usr/local/bin | Scripts for all users on this computer |
/usr/local/sbin | Scripts for the administrator |
/usr/bin | Only programs from the distribution's packages |
Onthoud
If ~/bin is not in your PATH, add export PATH=~/bin:"$PATH" to ~/.bashrc.
Write scripts that are easy to read
Long option names and line continuation make scripts clear for the next reader (that is often you in a year).
find playground \
\( \
-type f \
-not -perm 0600 \
-exec chmod 0600 '{}' ';' \
\) \
-or \
\( \
-type d \
-not -perm 0700 \
-exec chmod 0700 '{}' ';' \
\)- A
\at the end of a line continues the command on the next line. Nothing may follow the\, not even a space. - Indent the parts so the structure is visible.
Set up vim for scripts
A few vim settings help: colours, search highlight, auto indent and tabs as spaces.
syntax on
set hlsearch
set tabstop=4
set autoindent
set expandtab:syntax oncolours the shell syntax. If nothing changes, install the fullvimpackage.set expandtabwrites spaces instead of tab characters.
Summary of the chapter
Shebang + commands → chmod 755 → ~/bin. Write clearly: long options, \ for long lines, comments.
1What is the first line of a bash script and why?
#!/bin/bash (the shebang). It tells the kernel to run the file with bash.
2Why does hello_world not run when you are in its directory?
The current directory is not in PATH. Run ./hello_world or move it to ~/bin.
3What must follow a \ at the end of a line?
Nothing, not even a space; the command continues on the next line.