- Change language to:
The template: We write scripts all the time and we dont feel we have enough time to write the very same time after time after time, so we wrote a template, based on the idea of Rick Rosbag (which learned me almost everything for ksh programming).
#!/usr/local/bin/ksh
#################################################################
# In sorted order:
# 1. Prerequired info
# 2. Version History
# 3. Dependencies
# 4. Usefull Info
# 5. Variables
# 6. Functions
# 7. Actual Script
# 8. Creditlines
#################################################################
# 1. Prerequired info:
# RL = Remko Lodder
#################################################################
# 2. Version History
#
# Date? Who? What?
#
# 2002/10/05 RL Initial Version
#################################################################
# 3. Dependencies
#
# Date? Who? What?
#
# 2002/10/05 RL /bin/ksh and a template directory
#################################################################
# 4. Usefull Info
#
# Date? Who? What?
#
# 2002/10/05 RL This script is a template for all
# other scripts known in the clusters
#################################################################
# 5. Variables
#
DATE=`date "+%Y-%m-%d"`
SCRIPT=`basename $0`
LOCATION="/path/to/files"
LOGFILE="$LOCATION/$SCRIPT.$DATE.log"
#################################################################
# 6. Functions
#
### Function Name template_print
### Function Description This echoes the entire template
### into the read variable
function template_print
{
echo "
#################################################################
#!/bin/ksh
#################################################################
# In sorted order:
# 1. Prerequired info
# 2. Version History
# 3. Dependencies
# 4. Usefull Info
# 5. Variables
# 6. Functions
# 7. Actual Script
# 8. Creditlines
#################################################################
# 1. Prerequired info:
# RL = Remko Lodder
#################################################################
# 2. Version History
#
# Date? Who? What?
#
# $DATE $STARTER Initial Version
#################################################################
# 3. Dependencies
#
# Date? Who? What?
#
# $DATE $STARTER $DEPENDENCIES
#################################################################
# 4. Usefull Info
#
# Date? Who? What?
#
# $DATE $STARTER -
#################################################################
# 5. Variables
#
DATE=\`date \"+%Y-%m-%d\"\`
SCRIPT=\`basename \$0\`
LOCATION=\"/path/to/files\"
LOGFILE=\"/var/logs/\$SCRIPT.log\"
ERRORFILE=\"/var/logs/\$SCRIPT.error\"
#################################################################
# 6. Functions
#
### Function Name
### Function Description
### Function message, write message with script name and time in the logfile,
### This code is from Rick Rosbag!
### $* is a special variable which will print the rest of the options
### (like message bla ($* then is bla))
function message
{
echo \$SCRIPT: \`date +%Y-%d-%m@%H:%M\` hour: \$*
}
### Function trap_errors, trap errors in the script so we know what went wront,
### This code is from Rick Rosbag!
### $? is korn shell’s standard error return variable, (like exit 1 makes $? 1)
function trap_errors
{
ERROR=\$?
if [[ \"\$ERROR\" -ne \"0\" ]];
then
message Whoops, something went wrong in \$SCRIPT. It reported errors.
message The error code was \$ERROR.
message The linenumber that created this error is \$1
message Whoops, something went wrong in \$SCRIPT. It reported errors.
message The error code was \$ERROR >> \$ERRORFILE
message The linenumber that created this error is \$1 >> \$ERRORFILE
fi
}
#################################################################
# 7. Actual Script
#################################################################
### exec >> \$LOGFILE, write logging to \$LOGFILE
exec >> \$LOGFILE
exec 2>&1
message —————————————————————
trap ‘trap_errors \$LINENO’ ERR
message Descriptionhere starting
message subdescription here
function1
message description for function2
function2
message End of Script
message —————————————————————
#################################################################
# 8. Creditlines
# Copyright 2001-2004 Rick Rosbag (His code is mentioned!)
# The rest is Copyright 2001-2004 Remko Lodder (remko@elvandar.org)
#################################################################
" >> $TARGETFILE
}
### Function message, write message with script name and time in the logfile
### This code is from Rick Rosbag!
### $* is a special variable which will print the rest of the options
### (like message bla ($* then is bla))
function message
{
echo $SCRIPT: `date +%Y-%d-%m@%H:%M` hour: $*
}
### Function trap_errors, trap errors in the script so we know what went wront
### This code is from Rick Rosbag!
### $? is korn shell’s standard error return variable, (like exit 1 makes $? 1)
function trap_errors
{
ERROR=$?
if [[ "$ERROR" -ne "0" ]];
then
message Whoops, something went wrong in $SCRIPT. It reported errors.
message The error code was $ERROR.
message The linenumber that created this error is $1
message Whoops, something went wrong in $SCRIPT. It reported errors.
message The error code was $ERROR >> $ERRORFILE
message The linenumber that created this error is $1 >> $ERRORFILE
fi
}
### Function name read_initials
### Function description reads initials
function read_initials
{
echo "Would you be so kind to put in your initials?"
read STARTER
}
### Function name read_info
### Function description Read user info , they can give up their UserID and TARGETFILE
### also will be asked to put in any dependencies during time of
### writing and the purpose of the script.
function read_info
{
echo "Hi user, this will generate a template for your convienence."
read_initials
echo "Thanks $STARTER"
echo "What will be the targetfile of this script? It defaults to current directory,
echo "suggested is that you make a dedicated script directory"
read TARGETFILE
echo "Almost there"
echo "What are the current dependencies of the file?"
read DEPENDENCIES
echo "Alright Final question"
echo "Why was this script written?"
read REASON
}
#################################################################
# 7. Actual Script
#
#exec >> $LOGFILE
#exec 2>&1
trap ‘trap_errors $LINENO’ ERR
message template starting
read_info
template_print
message template created for $USER
#################################################################
# 8. Creditlines
#
# We wrote this template because of Rick Rosbag, he used it
# His code is included in thie file, see the remarks.
# It surely speed’s up the process of creating a lot of the
# same files with same checks.
# Copyright 2001-2004 Rick Rosbag (His code is mentioned)
# The rest is Copyright 2001-2004 Remko Lodder (remko@elvandar.org)
#################################################################

