Exercise
Preventing Clobber Exercise
Task
Stop > from silently destroying a file
you meant to keep, then make that protection permanent.
- Hosts
-
workstation, thenservera
-
Create a file holding data you care about
cd ~ echo "quarterly revenue: 4200" > finance_report.txt cat finance_report.txt -
Enable noclobber and try to overwrite it
set -owith no arguments lists every shell option and whether it is on.set -o | grep noclobber set -o noclobberNow make the mistake
noclobberexists to prevent. The redirect is refused and the file survives.echo "oops, wrong file" > finance_report.txt echo $? cat finance_report.txt -
Append, then overwrite on purpose
Appending is never destructive, so
noclobberallows it.echo "quarterly expenses: 3100" >> finance_report.txt cat finance_report.txt>|says you really do mean to overwrite.noclobberis a seatbelt, not a lock.echo "superseded by the audited figures" >| finance_report.txt cat finance_report.txt -
Confirm the option is off again in a new session
Log out of
serveraand log back in.exit ssh servera set -o | grep noclobber -
Persist noclobber in ~/.bashrc
vim ~/.bashrcset -o noclobberexit ssh servera set -o | grep noclobber echo "test" > finance_report.txtClean up
rm finance_report.txt