#!/bin/bash # ============================= Functions ============================ ############################## Debugging Functions ###################### function BUGPAUSE { echo " " echo "-----------------------------------------------------------" echo " MANUAL BUG CHECK!" echo " Is the program doing what you expect? Please " echo " review preceeding output for correctness. Thank you." echo "-----------------------------------------------------------" echo " " ask "Continue? [Y/n]" if [ $? -ne 0 ]; then { exit 100 } fi } #### #### # # Error Handler. # #### #### function ERROR { clear echo "----------------" echo "| ABEND: Code $1 |" echo "-------------------------------------------------------------------------" echo " Bummer! An error occurred!" echo " Don't you just hate it when..." echo "${2}?" echo "==========================" echo " Maybe this will help...." echo "==========================" SYNTAX exit $1 } #### #### # # - SYNTAX -------- Bet you cannot guess what this does. # #### #### function SYNTAX { ask "Do you wish to view the Syntax for VERSION?" if [ $? -ne 0 ]; then return fi echo "SYNTAX:" echo "Program $0" echo " $0 [arg1]" echo " Will cause $0 to create a copy of the script newscript_template file with" echo " appropriate permissions set." echo " You may then use the newscript_template file to create a new shell script." echo " " echo " This should make shell-scripting far easier as well as standardizing" echo " many of the routine modules and functions that should be in most scripts." echo "NOTES:" echo " ${0}'s script newscript_template file is stored in " echo " /root/bin/newscript_template, usually with a spare in /root/bin." echo " This file can be updated or altered with a simple" echo " text editor to suit your preferences." ask "Do you wish to continue?" if [ $? -ne 0 ]; then return fi exit 0 } #### #### # # - CHECKCMD --------- Locates the system cammands used by this script.------- # This makes sure the commands we need exist. If a command is missing, # this function will cause the script to abort. # #### #### function CHECKCMD { # Check for required *NIX executable commands. echo "---->In CHECKCMD...." for i in id seq echo hostname touch cp mv rm rsync mount smbmount; do which $i; if [ $? -ne 0 ]; then ERROR 1 "the command $i is missing from the system" fi; done ## Check for individual specifically required files if [ -f /root/bin/newscript_template ]; then echo 'Master script newscript_template found. Proceeding.' else ERROR 2 'when /root/bin/newscript_template is missing or not in the current directory' fi } #### #### # # - R_U_ROOT ----- Aborts the program if the person running it is not root. # #### #### function R_U_ROOT { ID=`id -u` if [ $ID -ne 0 ]; then { echo "$0 is a potentially dangerous program." echo "Therefore it is only to be run by the root user." echo "You appear to be running it as `id -nu`." echo "Please try again as root or have the system administrator run" echo "$0 for you." ERROR 10 "you forget to log in as root first" } fi } #### #### # # - GETARGS ----------- Converts command-line args to appropriate parameters. Otherwise, # it prompts for the necessary arguments. # #### #### function GETARGS { echo "IN GETARGS...." if [ -z $1 ]; then { echo "What would you like to name your new shell-script?" read NEWSCRIPT if [ -z $NEWSCRIPT ]; then ERROR 30 "the program won't continue because you didn't specify a name for the new shell-script" fi } else NEWSCRIPT=$1 fi } #### #### # # Reports the version for this program. # #### #### function VERSION { echo "$0 Version 10292k3-c" } #======================================= No more functions. ========== ############################################################################# ################################# #################################### ################################# MAIN #################################### ################################# #################################### ############################################################################# case "$1" in v|V|-v|-V|--version|--VERSION|--ver|-version|-VERSION|-Version|-VER|-ver|-Ver|--Ver|--VER|--Version) VERSION exit 0 ;; h|H|-h|-H|--help|--HELP|HELP|help|Help|--Help) SYNTAX exit 0 ;; esac R_U_ROOT VERSION CHECKCMD GETARGS $1 #---------------------------------------------------------------------- #TEST-SWITCHES #Comment out for normal use. #SERVER=stjoseph #FILESYSTEM=d-stjoseph # ------------- file locations ----------------------------------------- echo "IN MAIN-------->>>>>>>" if [ -f /root/bin/$1 ]; then ERROR 100 "you discover that $1 already exists" fi cp /root/bin/newscript_template /root/bin/$1 if [ $? -ne 0 ]; then ERROR 101 "the copy command fails because I cannot find /root/bin/newscript_template" fi chmod 755 /root/bin/$1 if [ $? -ne 0 ]; then ERROR 102 "The permissions change for $1 fails" fi echo "New $1 file now mastered in /root/bin." echo "$0: End run--------------<<<<<<<".