Annotated Line by Line analysis of Log Script Development

In this, 'type>' represents your terminal prompt; Ctrl-d means press the Ctrl key and the 'd' key at the same time.

What you need to do How you can do it
get into your home directory type> cd
save script in a file named log type> cat 1>log
the first line says to use bash to interpret this script #!/bin/bash
define the name and location of the logfile (use lower case to avoid accidental confusion with environment variables that are defined and used by the shell) logfile=$HOME/logged.comments
deal with the first parameter, which is the variable named 1, whose value is accessed by referring to it as $1 case $1 in
if it is null (represented by an opening quote, followed by nothing at all, and then a closing quote) prompt for the text and save it in a variable named 'comment' "") read -p "log text: < " comment
create a day stamp for this text day=$(/bin/date -R|tr -d ','|awk '{print $1,$2,$3}')
append the day stamp and the text of the comment to the logfile echo "$day: $comment">>$logfile
check that the comment was added to the end of the logfile correctly tail -1 $logfile
remind yourself of the name of the logfile, and quit the case statement echo "logfile is $logfile" ;;
if you typed 'log -v' or 'log v' then view file, and quit the case statement -v|v) less $logfile ;;
if you typed `log -e' or 'log e' then edit the file, and quit the case statement -e|e) nano $logfile ;;
if you typed `log -p' or 'log p' then print the file, check the printer queue, and quit the case statement -e|e) a2ps $logfile; lpq ;;
if there were any other arguments, they are mistakes, so just print out a reminder of how to use this script, and quit the case statement *) echo "use: log [-e|-v|-p], blank prompts for comment" ;;
case done esac
exit script exit 0
close and save the script file Ctrl-d
point to this log script type> alias log='$HOME/log'
make alias permanent (between log and >> that is a single quote followed by a double quote) type> echo "alias log='$HOME/log'">>$HOME/.bashrc
make script executable type> chmod +x log
try it out from any directory type> log
log text: < installed w3m to get text from .html
Mon 12 Jul: installed w3m to get text from .html
logfile is /home/user/logged.comments


LSGA Tutorial: "Develop a Log Script" is copyright © Dr Gonzo Publishing 2008