Geavanceerd
Zoeken inLabs
Zo zoek je
  • Zoek een taak of een tool (lvm, selinux, @podman); filter op niveau en op boek of cursus.
  • Met een leeg zoekvak toont een filter alleen al de labs — bijvoorbeeld alle moeilijke labs van één cursus.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Lab 13.1The Linux Command Line, Hoofdstuk 13

Part 2 practice: your own environment, vi and prompt

Niveau
Makkelijk
30 min
Machines
any Linux system
Status
Nog niet begonnen

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

Doel

Set up a comfortable working environment that stays after logout: a personal bin directory in the PATH, aliases and history settings, a coloured prompt with the time, and a few edits in vi.

Voordat je begint

Taken

  1. Make the directory ~/bin and make sure it is in your PATH in every new shell.
  2. Add to ~/.bashrc: the alias ll for ls -l --color=auto, and history settings that ignore repeated commands and keep 5000 lines. Add a comment above each line.
  3. Load the changes into the open shell without logging out.
  4. Show the date in the time zone Europe/Amsterdam for one command only. Your normal TZ must not change.
  5. Copy /etc/services to ~/services.txt. In vi: go to line 20, delete 3 lines, replace every udp with UDP in the whole file, and save.
  6. Make a green prompt that shows the time (hours:minutes), the user, the host and the last part of the directory. Make it stay in new shells.

Controleer je werk

bash -lc 'echo $PATH' | tr ':' '\n' | grep bin
bash -ic 'type ll'
bash -ic 'echo $HISTCONTROL $HISTSIZE'
grep -c UDP ~/services.txt
bash -ic 'echo "$PS1"'
  • PATH shows /home/USER/bin.
  • type ll says it is an alias.
  • HISTCONTROL is ignoredups and HISTSIZE is 5000.
  • The prompt contains \A, \u, \h, \W and the colour code 0;32.

Hints

Toon de hints
  • PATH changes go in ~/.bash_profile (or ~/.profile); aliases in ~/.bashrc.
  • source ~/.bashrc reloads the file.
  • NAME=value command sets a variable for one command.
  • In vi: 20G, 3dd, :%s/udp/UDP/g, :wq.
  • Colours need \[ and \] around them.

Oplossing

Toon de oplossing
  1. Make the directory and add it to the PATH.
    mkdir -p ~/bin
    echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bash_profile
  2. Add the aliases and history settings with comments.
    cat >> ~/.bashrc << 'EOF'
    # my aliases
    alias ll='ls -l --color=auto'
    # history: no repeated lines, keep more
    export HISTCONTROL=ignoredups
    export HISTSIZE=5000
    EOF
  3. Load the file.
    source ~/.bashrc
  4. A temporary variable.
    TZ=Europe/Amsterdam date
    echo "TZ is still: '$TZ'"
  5. Edit in vi (type the keys in vi).
    cp /etc/services ~/services.txt
    vi ~/services.txt
    keys in vi
    20G
    3dd
    :%s/udp/UDP/g
    :wq
  6. Make the prompt and save it.
    echo 'PS1="\[\033[0;32m\]\A \u@\h \W\$\[\033[0m\] "' >> ~/.bashrc
    source ~/.bashrc

Mijn notities

Nog geen notities.

Lessen voor dit lab

Commando's in dit lab