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 24Naam in het boek: Writing Your First Script

Write your first script

A shell script is a text file with commands. Everything you type on the command line can go into a script, and everything in a script can be typed on the command line. Scripts turn long, repeated work into one command. This chapter shows the three steps: write the file, make it executable, put it where the shell finds it.

  • 7 Lessen
  • 8 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 script schrijven, uitvoeren en plaatsen zodat het werkt als een commando

24.1

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.

24.2

Write the script file

A script needs three things: a text file with the commands, execute permission, and a place in the PATH.

hello_world
#!/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").

24.3

Make it executable

The file needs the x permission. 755 lets everyone run it; 700 only you.

ls -l hello_world
-rw-r--r--. 1 me me 63 Nov  8 hello_world
chmod 755 hello_world
ls -l hello_world
-rwxr-xr-x. 1 me me 63 Nov  8 hello_world
./hello_world
Hello World!

Notitie

A script must also be readable to run, because the interpreter reads the file.

24.4

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"
PlaceFor
~/bin or ~/.local/binYour own scripts
/usr/local/binScripts for all users on this computer
/usr/local/sbinScripts for the administrator
/usr/binOnly programs from the distribution's packages

Onthoud

If ~/bin is not in your PATH, add export PATH=~/bin:"$PATH" to ~/.bashrc.

24.5

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).

ls -ad
ls --all --directory
long command, split with \ and indent
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.

Commando's in deze les

24.6

Set up vim for scripts

A few vim settings help: colours, search highlight, auto indent and tabs as spaces.

~/.vimrc
syntax on
set hlsearch
set tabstop=4
set autoindent
set expandtab
  • :syntax on colours the shell syntax. If nothing changes, install the full vim package.
  • set expandtab writes spaces instead of tab characters.

24.7

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.

Oefenen

Alle commando's in dit hoofdstuk