#!/bin/ksh ## ## SCRIPT NAME: set_localoutlist ## ## Where: in $FEDIR/scripts where FEDIR = /apps/nns_com/fea ## ############################################################################# ## PURPOSE: ## Sets OUTLIST="/local/scratch/$USER/temp.lis####", if that directory ## exists. ## Otherwise, it sets OUTLIST="$HOME/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 ## ############################################################################# ## WHERE USED: ## 'set_localoutlist' ## is used by sho* cmds (like shofil, i.e. nnsFEAmenu help options) ## and nnsFEAmenu FileUtils, PrtUtils, HostConfig, HostVu, NetVu options. ## ## Can use ./FIND_NOT_IN_OLD 'set_localoutlist' ## in directory $FEDIR/scripts ( & $FEDIR/tkGUIs ) ## to show ksh or tk scripts that use 'set_localoutlist'. ## ############################################################################# ## MAINTENANCE HISTORY: ## Written by: Blaise Montandon 8Nov1996 ## Updated by: Blaise Montandon 28Jul1997 Added mkdir if directory ## /local/scratch/$USER does not exist. ## Updated by: Blaise Montandon 1oct1999 Just standardized this doc text. ## Updated by: Blaise Montandon 10Nov2000 Added MINS-SECS to temp.lis name. ## Updated by: Blaise Montandon 13Nov2000 Chg 'test -d' to 'test -w' and ## prep script to remove old temp.lis ## files (add OUTDIR var). ## Updated by: Blaise Montandon 18dec2000 Activated 'find'-and-remove old ## temp.lis[0-6][0-9][0-6][0-9]* files. ## Updated by: Blaise Montandon 22dec2000 Add '-local' to the 'find' command. ## Updated by: Blaise Montandon 29mar2001 Add a directory prune, namely ## '\( -type d ! -name "$USER" -prune \) -o' ## to the 'find ... -exec rm {}' command. ## Updated by: Blaise Montandon 22mar2004 Chg 'rm ' to 'rm -f '. ## ## Updated by: Blaise Montandon 05oct2004 1) Chg FROM using a 'find' command ## to remove-temp-files-older-than ## one week -- TO using 'ls -t' ## to remove all but N, with N~50. ## 2) Add a section to remove temp ## files that are 'big', say bigger ## than 50 Meg. ## Updated by: Blaise Montandon 06oct2004 Add check on SECS to do the removes ## of old-and-big temp.lis files about ## one-half or one-quarter of the calls ## to this script. ############################################################################# ############################################################################# ## MAKE $USER SUBDIRECTORY of /local/scratch, if it does not exist. ############################################################################# if test ! -d /local/scratch/$USER then mkdir /local/scratch/$USER chmod 777 /local/scratch/$USER 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 /local/scratch/$USER ## if it exists, otherwise in $HOME. 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 /local/scratch/$USER then OUTDIR="/local/scratch/$USER" OUTLIST="$OUTDIR/$TEMP_FILENAME" else echo " Could not find or create directory /local/scratch/$USER with write permission for $USER. Using $HOME for temporary list file. " OUTDIR="$HOME" OUTLIST="$OUTDIR/$TEMP_FILENAME" fi ########################################################################### ## 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 /local/scratch/$USER or $HOME. ########################################################################## ## We used to use the 'find' command, but it is difficult to keep ## the find command from traveling through subdirectories of ## /local/scratch/$USER or $HOME --- 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` TEMPLISFILES=`ls -t $OUTDIR | grep '^temp\.lis[0-6][0-9][0-6][0-9]*' | tail +$NFILES_TAIL` for RELFILE in $TEMPLISFILES do ## FOR TESTING: # ls -l $OUTDIR/$RELFILE # echo "NOT a MOST-RECENT-${NFILES_KEEP} FILE." rm -f $OUTDIR/$RELFILE done ########################################################################## ## 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