#!/bin/bash # Author: Joshua Bailey # Script: divApp2 # This script takes the first argument you entered # and writes to a file with that name. # If it exists it prompts to append, overwrite, or exit # It then opens the other file names you pass it to redirect # Their output for the first files input. # syntax: divApp2 fileAddTo addFrom1 addFrom2 addFrom3 .... # function printBeginning printBeginning () { # append a line break echo "================================================================" >> $1 # append begin echo " <*** Begin \"$2\" ***>" >> $1 # space holder echo " " >> $1 } # end function printBeginning # function printEnding printEnding () { # space holder echo " " >> $1 # append the end of file line break echo " <*** End \"$2\" ***>" >> $1 echo "================================================================" >> $1 # space holder echo " " >> $1 echo " " >> $1 } # end function printEnding # function argumentCheck argumentCheck () { # check to see if user used enough arguments if [[ ! $1 > 1 ]] then # tell the user that the syntax was entered incorrectly echo "*** Error: too few arguments!" echo "*** Must have a file to append to or create." echo "*** Must also have atleast one file to read from." echo "*** Syntax: divApp2 fileAddTo addFrom1 addFrom2 addFrom3..." exit fi } # end function argumentCheck # function existFileAddTo existFileAddTo () { if [[ -e $1 ]] then # if it exists say so echo "File \"$1\" already exists." # ask user what they want to do read -p "Do you want to append, overwrite, or exit? (a|o|e) " ans # make sure the variable holds the correct case ans=`echo $ans | tr [:upper:] [:lower:]` # use only the first letter to decide what to do ans=${ans:0:1} # do what the user requested if [[ $ans = "e" ]] then echo "*** Exiting ***" exit elif [[ $ans = "o" ]] then echo "*** Overwriting ***" rm -f $1 ans="T" elif [[ $ans = "a" ]] then echo "*** Appending ***" ans="T" else if [[ $ans != "T" ]] then echo "That choice is not an option!" echo "*** Exiting ***" exit fi fi fi } # end function existFileAddTo # function viewFile viewFile () { read -p "Do you want to display the file? (y|n) " YesNo # make user input lower case YesNo=`echo $YesNo | tr [:upper:] [:lower:]` # cut off all except the first letter YesNo=${YesNo:0:1} if [[ $YesNo = "y" ]] then # display the file cat $1 | less fi } # end function viewFile # assign the first parameter (the created file) to copyTo copyTo=$1 # assign the number of parameters to loopCount loopCount=$# # declare the array declare -a arr # check to see if user used enough arguments argumentCheck $loopCount # assign the arguments to an array for ((i=0; i != ($loopCount-1); i++)) do arr[$i]=$2 shift 1 # see if all files listed exist...exit if they don't if [[ ! -e ${arr[$i]} ]] then echo "File \"${arr[$i]}\" does not exist, or you typed the wrong path!" echo "*** Aborting ***" exit fi done # see if the fileAddTo file exists or not existFileAddTo $copyTo # loop the appending the number of parameters minus one (the first one) for ((i=0; i < `echo ${#arr[*]}`; i++)) do printBeginning $copyTo ${arr[$i]} # append the contents of the file you specified cat ${arr[$i]} >> $copyTo printEnding $copyTo ${arr[$i]} done # state that the process is complete echo "*** Done ***" # see if user wants to view the file created viewFile $copyTo