Register
It is currently Thu Nov 27, 2014 11:34 am

BASH SCRIPT : Better than cat >


All times are UTC


Post new topic Reply to topic  [ 9 posts ] 
Author Message
 PostPosted: Sun Jan 18, 2004 5:44 am   
Moderator
User avatar

Joined: Tue Dec 02, 2003 7:48 pm
Posts: 1723
Location: Georgia
Code:
#!/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

to think.....this only took 3 days to write


Last edited by jbsnake on Sat Aug 07, 2004 5:51 pm, edited 3 times in total.

Top
 Profile  
 PostPosted: Wed Feb 18, 2004 7:02 pm   
Moderator
User avatar

Joined: Tue Dec 02, 2003 7:48 pm
Posts: 1723
Location: Georgia
this script was written with log files in mind...i think it's good for taking multiple log files that have to be looked at on a semi-daily basis...you can edit it to include date stamps and whatnot on the dividing sections (printBeginning and printEnding) which would help with organizing...this would be a good cron candidate for making reports using different log files.
like setting cron to run the script as
Code:
todaytag=`date`
divApp2 logfiletomake.$todaytag logfile1 logfile2 logfile3...

doing that would take the log files on a regular basis and appending them all to a file with the current date string (i would shorten it personally by editing the string with different tags for the date app)
it also makes printing the different logs easy (saves paper too :) )


Top
 Profile  
 PostPosted: Thu Feb 19, 2004 2:52 am   
Moderator
User avatar

Joined: Sun Nov 02, 2003 6:44 am
Posts: 1027
Location: Hertford, NC
Now thats something I can probably put to use :)


Top
 Profile WWW ICQ  
 PostPosted: Thu Feb 19, 2004 10:44 pm   
Moderator
User avatar

Joined: Tue Dec 02, 2003 7:48 pm
Posts: 1723
Location: Georgia
to think...it was the first script i posted :)


Top
 Profile  
 PostPosted: Fri Feb 20, 2004 4:33 am   
Moderator
User avatar

Joined: Sun Nov 02, 2003 6:44 am
Posts: 1027
Location: Hertford, NC
lol, was probably in a hury and overlooked it. Somethign about 1000+ emails in the inbox, a daytime job, sys admin at night and a grandbaby running around one week out of the month that can make you loose track of things :)


Top
 Profile WWW ICQ  
 PostPosted: Fri Feb 20, 2004 4:34 am   
Moderator
User avatar

Joined: Sun Nov 02, 2003 6:44 am
Posts: 1027
Location: Hertford, NC
Very nice script. I'll have to look more at it this weekend.


Top
 Profile WWW ICQ  
 PostPosted: Fri Feb 20, 2004 4:40 am   
Moderator
User avatar

Joined: Tue Dec 02, 2003 7:48 pm
Posts: 1723
Location: Georgia
co0...hope you enjoy it (or can atleast use it at one point) :)


Top
 Profile  
 PostPosted: Fri Feb 20, 2004 5:05 am   
Moderator
User avatar

Joined: Sun Nov 02, 2003 6:44 am
Posts: 1027
Location: Hertford, NC
I already have a logfile viewer but I might use this for other logs and for anything I need custom, and set up another script to email it to me every day.


Top
 Profile WWW ICQ  
 PostPosted: Sat Sep 07, 2013 10:09 am   
Moderator
User avatar

Joined: Tue Dec 02, 2003 7:48 pm
Posts: 1723
Location: Georgia
9 years later... and I'd still write this the same way...

Except I'd have a case statement in the function: existFileAddTo ()

My how times have changed...

_________________
server: Arch Linux - Kernel 2.6.33-ARCH
Lenovo T60 StinkPad: Arch Linux - Kernel 3.10.10-1-ARCH
VirtualBox 4.2.18 Hosted on MacOS X (10.7.5) Mountain Lion: Arch Linux - Kernel 3.10.10-1-ARCH


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


Powered by phpBB © 2012 phpBB Group
© 2003 - 2012 USA LINUX USERS GROUP