Chapter 5: Text, editors and the shell environment

Exercise: Text and environment review

Produce four small reports on servera with redirection, pipes and grep, write a notice with vim, and set up your shell so that three settings survive every new login. lab grade checks each result.

Text and environment review

  • Separate output from errors into files.
  • Select exactly the right lines with grep.
  • Edit files with vim and make shell settings permanent.

Before you start: reset the servers and complete this chapter's lessons. You work on servera as student; lab start and lab grade run on workstation.

On servera, as student, create ~/reports, then:

  1. bash-users.txt: every line of /etc/passwd for an account whose login shell is /bin/bash, and no other line.
  2. repos.txt: the .repo files under /etc, found with find, with no error messages in the file.
  3. find-errors.txt: the error messages from that same find command.
  4. chrony-active.txt: the lines of /etc/chrony.conf that are neither comments nor empty.
  5. ~/notice.txt, written with vim, containing exactly: Welcome to servera.lab.example.com. and Contact: oncall@lab.example.com.
  6. In ~/.bashrc: an alias now that runs date +%T, and HISTTIMEFORMAT exported as "%F %T ".
  7. In ~/.bash_profile: EDITOR exported as vim.

Start the exercise

[student@workstation ~]$ lab start sa-text-review
[student@workstation ~]$ ssh student@servera
[student@servera ~]$ mkdir ~/reports

Report the Bash users

[student@servera ~]$ grep '/bin/bash

Loading…

#39; /etc/passwd > ~/reports/bash-users.txt [student@servera ~]$ cat ~/reports/bash-users.txt root:x:0:0:root:/root:/bin/bash student:x:1000:1000:Student User:/home/student:/bin/bash

Anchoring to the end of the line matters: a plain bash would also match an account whose home directory or comment happened to contain the word.

Find the repository files, keeping errors apart

[student@servera ~]$ find /etc -name "*.repo" > ~/reports/repos.txt 2> ~/reports/find-errors.txt
[student@servera ~]$ wc -l ~/reports/repos.txt ~/reports/find-errors.txt
  5 /home/student/reports/repos.txt
 13 /home/student/reports/find-errors.txt
 18 total

Extract chrony's active settings

[student@servera ~]$ grep -Ev '^(#|$)' /etc/chrony.conf > ~/reports/chrony-active.txt
[student@servera ~]$ cat ~/reports/chrony-active.txt
pool 2.rocky.pool.ntp.org iburst
sourcedir /run/chrony-dhcp
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
keyfile /etc/chrony.keys
ntsdumpdir /var/lib/chrony
leapsectz right/UTC
logdir /var/log/chrony

Two pipelined greps, grep -v '^#' … | grep -v '^

Loading…

#39;, give the same result.

Write the notice with vim

vim ~/notice.txt, press i, type the two lines, press Esc and :wq.

[student@servera ~]$ cat ~/notice.txt
Welcome to servera.lab.example.com.
Contact: oncall@lab.example.com

Make the shell settings permanent

Add two lines to the end of ~/.bashrc (vim ~/.bashrc, G, o):

alias now='date +%T'
export HISTTIMEFORMAT="%F %T "

and one to the end of ~/.bash_profile:

export EDITOR=vim

Check from a fresh login:

[student@servera ~]$ exit
[student@workstation ~]$ ssh student@servera
[student@servera ~]$ type now
now is aliased to `date +%T'
[student@servera ~]$ echo $EDITOR
vim
[student@servera ~]$ exit

Grade and finish

Check your work with lab grade sa-text-review, then put the exercise away with lab finish sa-text-review.