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:
bash-users.txt: every line of/etc/passwdfor an account whose login shell is/bin/bash, and no other line.repos.txt: the.repofiles under/etc, found withfind, with no error messages in the file.find-errors.txt: the error messages from that samefindcommand.chrony-active.txt: the lines of/etc/chrony.confthat are neither comments nor empty.~/notice.txt, written with vim, containing exactly:Welcome to servera.lab.example.com.andContact: oncall@lab.example.com.- In
~/.bashrc: an aliasnowthat runsdate +%T, andHISTTIMEFORMATexported as"%F %T ". - In
~/.bash_profile:EDITORexported asvim.
Start the exercise
[student@workstation ~]$ lab start sa-text-review
[student@workstation ~]$ ssh student@servera
[student@servera ~]$ mkdir ~/reportsReport the Bash users
[student@servera ~]$ grep '/bin/bashLoading…
#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/bashAnchoring 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 totalExtract 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/chronyTwo pipelined greps, Loading…grep -v '^#' … | grep -v '^, 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.comMake 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=vimCheck 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 ~]$ exitGrade and finish
Check your work with lab grade sa-text-review, then put the exercise away with lab finish sa-text-review.