#!/bin/sh ## ## SCRIPT NAME: set_localoutlist.sh ## ## Where: in $FE_SCRIPTS_DIR=$FEDIR/scripts ## ## where $FEDIR represents the install directory of an FE subsystem. ## ############################################################################# ## PURPOSE: ## Sets OUTLIST="/tmp/$USER/temp.lis####", if the /tmp/$USER directory ## exists. ## Otherwise, it sets OUTLIST="$HOME/tmp/temp.lis####". ## ## Here #### represents 4 integers generated near-randomly from the ## minutes & seconds in the time of day. ## ## In addition, to avoid accumulation to 'temp.lis' files, there are ## two sections at the bottom of the script to remove ## ## a) all but the N most-recent 'temp.lis####...' files, where N is ## an integer like 50 or 100. ## b) the big 'temp.lis####...' files, where 'big' is say > 50Meg. ############################################################################# ## TYPICAL CALL FORMAT: . $FEDIR/scripts/set_localoutlist.sh ## ############################################################################# ## WHERE USED: ## 'set_localoutlist.sh' is used by FE subsystem scripts, like the ## 'findANDshow_stringINfile_plusminusNlines.sh' ## script of the FE 'xpg' subsystem. ## ## You can use the command ## grep 'set_localoutlist' * ## in a $FEDIR/scripts directory to show scripts that use ## 'set_localoutlist.sh'. ## ############################################################################# ## MAINTENANCE HISTORY: ## Updated by: Blaise Montandon 2008mar24 Started the 'set_localoutlist.sh' ## script on Linux, using Mandriva2007. ## Updated by: Blaise Montandon 2009nov11 Restarted development, on Ubuntu. ## Found that 'tail +N' does not work ## on Ubuntu Karmic (9.10). ## Used a 'tail -N' technique to get ## all but the first 100 recs in a ## time-sorted list of the temp files. ## Updated by: Blaise Montandon 2010sep06 Touched up comments for an initial ## release of the FE 'xpg' subsystem. ############################################################################# TEMPDIR="/tmp" ############################################################################# ## MAKE $USER SUBDIRECTORY of $TEMPDIR, if it does not exist. ############################################################################# if test ! -d $TEMPDIR/$USER then mkdir $TEMPDIR/$USER chmod 777 $TEMPDIR/$USER ## We want to allow other users of the computer to delete ## 'temp' files created in this user subdirectory of /tmp. fi ############################################################################# ## MAKE NAME OF (TEMPORARY,SCRATCH) LIST FILE. ## Include a time-stamp to avoid overlaying other temp.lis files being ## currently viewed (and perhaps printed). ############################################################################# SECS=`date +%S` MINS=`date +%M` TEMP_FILENAME="temp.lis${MINS}${SECS}" ############################################################################# ## MAKE THE FULLNAME OF THE LIST FILE --- in $TEMPDIR/$USER ## if it exists, otherwise in $HOME/tmp. The filename is in var OUTLIST. ## ## By calling this script with 'dot' --- . $FEDIR/scripts/set_localoutlist ## --- the OUTLIST var is available to the calling script. ############################################################################# if test -w $TEMPDIR/$USER then OUTDIR="$TEMPDIR/$USER" OUTLIST="$OUTDIR/$TEMP_FILENAME" else echo " Could not find or create directory $TEMPDIR/$USER with write permission for $USER. Using $HOME/tmp for temporary list file. " OUTDIR="$HOME/tmp" if test ! -d $OUTDIR then mkdir $OUTDIR chmod 755 $OUTDIR/$USER fi OUTLIST="$OUTDIR/$TEMP_FILENAME" fi ## FOR TESTING: # set -x ########################################################################### ## AT this point $OUTLIST is set. Now cleanup old & big 'temp.lis####' files ## ... but do it only about half or 1/4 the time, based on current SECS. ########################################################################### # if test \( $SECS -gt 10 -a $SECS -lt 41 \) # if test $SECS -lt 15 if test $SECS -lt 30 then ########################################################################## ## Do cleanup in two sections: ## 1) Use 'ls -t' to remove all but N such files , where N=50, say. ## 2) Use 'ls -l' to remove temp files that are 'big', say bigger than 50 Meg. ########################################################################## ########################################################################## ## REMOVE ALL BUT 'N-most-recent' 'temp.lis####' FILES: ## ## to help avoid accumulation of up to 60x60=3600 temp.list files ## in $TEMPDIR/$USER or $HOME/tmp. ########################################################################## ## We used to use the 'find' command, but it is difficult to keep ## the find command from traveling through subdirectories of ## $TEMPDIR/$USER or $HOME/tmp --- in a way that avoids using 'cd'. ########################################################################## ## We will use 'ls -t' and 'grep' and 'tail' once --- ## and we use 'rm' in a 'for' loop, even though we would ## like to avoid the 'for' loop. Since there will generally be less ## than 100 files, the loop should always go quickly. ########################################################################## ## We can test with 'ls -l' in place of 'rm -f'. ########################################################################## NFILES_KEEP=100 # NFILES_KEEP=50 NFILES_TAIL=`expr $NFILES_KEEP + 1` ## The +N feature of 'tail' is not working, on Ubuntu Karmic (9.10). ## Getting the following error msg: ## tail: cannot open `+101' for reading: No such file or directory # TEMPLISFILES=`ls -t $OUTDIR | grep '^temp\.lis[0-6][0-9][0-6][0-9]*' | tail +$NFILES_TAIL -` ## An alternative technique: TOTFILES=`ls -t $OUTDIR | grep '^temp\.lis[0-6][0-9][0-6][0-9]*' | wc -l` if test $TOTFILES -gt $NFILES_KEEP then FILES2RM=`expr $TOTFILES - $NFILES_KEEP` TEMPLISFILES=`ls -t $OUTDIR | grep '^temp\.lis[0-6][0-9][0-6][0-9]*' | tail -$FILE2RM` for RELFILE in $TEMPLISFILES do ## FOR TESTING: # ls -l $OUTDIR/$RELFILE # echo "NOT a MOST-RECENT-${NFILES_KEEP} FILE." rm -f $OUTDIR/$RELFILE done fi ########################################################################## ## REMOVE BIG 'temp.lis####' FILES: ########################################################################## ## As above, we avoid using a 'find' command with the '-size' parm. ########################################################################## ## We will use 'ls' and 'grep' once and then we use ## both 'ls -l' and 'rm' in a 'for' loop, even though we would ## like to avoid the 'for' loop. Since there will generally be less ## than 100 files, the loop should always go quickly. ########################################################################## ## We can test with 'ls -l' in place of 'rm -f'. ########################################################################## ###################### ## Define Big = 10 Meg ###################### # BIGinBYTES=10000000 ###################### ## Define Big = 50 Meg ###################### BIGinBYTES=50000000 ## FOR TESTING: (10 KB) # BIGinBYTES=10000 TEMPLISFILES=`ls $OUTDIR | grep '^temp\.lis[0-6][0-9][0-6][0-9]*'` for RELFILE in $TEMPLISFILES do ## FOR TESTING: # set -x RELFILE_BYTES=`ls -l $OUTDIR/$RELFILE | awk '{print $5}'` if test $RELFILE_BYTES -gt $BIGinBYTES then ## FOR TESTING: # ls -l $OUTDIR/$RELFILE # echo "BIG FILE." rm -f $OUTDIR/$RELFILE fi ## FOR TESTING: # set - done fi ## END OF if test $SECS -lt 30