blob: 3b1d6a1d871e1d8efa2c2aa988e686ca23a489d9 [file] [log] [blame]
david_williams6d16e662011-02-12 19:56:12 +00001#!/usr/bin/env bash
2
3# simple function that can be called via 'find' that removes and displays name of removed directory
4function removeIf ()
5{
6 # echo "arg: $1";
7
david_williamsca2f4c52011-02-12 21:24:18 +00008 foundDirectory=$1
9 if [ -z "${foundDirectory}" ]
david_williams6d16e662011-02-12 19:56:12 +000010 then
11 echo "ERROR: No argument. This function requires a directory as an argument. " ;
12 return 1;
13 fi
david_williamsca2f4c52011-02-12 21:24:18 +000014
david_williams6d16e662011-02-12 19:56:12 +000015
david_williamsca2f4c52011-02-12 21:24:18 +000016 if [ ! \( -d "${foundDirectory}" \) ]
david_williams6d16e662011-02-12 19:56:12 +000017 then
18 echo "ERROR: " "${foundDirectory}" ", is not a directory. This function requires a directory as an argument. "
19 return 2;
20 fi
21
22 # should already be in foundDirectory, if execDir used (and execDir, in a 'find' is recommended, as more secure)
david_williamsca2f4c52011-02-12 21:24:18 +000023 #cd $foundDirectory
david_williams6d16e662011-02-12 19:56:12 +000024
david_williamsca2f4c52011-02-12 21:24:18 +000025 rm -fr "${foundDirectory}"
david_williamse2c12572011-02-12 20:53:03 +000026 rcode=$?
27 if [ $rcode -eq 0 ]
david_williams6d16e662011-02-12 19:56:12 +000028 then
david_williams54e28002011-02-12 21:34:36 +000029 echo " removed: " "${foundDirectory}";
david_williams6d16e662011-02-12 19:56:12 +000030 else
david_williamsca2f4c52011-02-12 21:24:18 +000031 echo "WARNING: rm returned non zero return code: " $rccode ". could not remove " "${foundDirectory}";
david_williams6d16e662011-02-12 19:56:12 +000032 fi
33
david_williamse2c12572011-02-12 20:53:03 +000034 return $rcode;
david_williams6d16e662011-02-12 19:56:12 +000035}
36
37# function that can remove a directory (e.g. via find) but checks to make sure not to remove the last one (or, last 'saveAtLeast')
david_williams1875e482011-02-12 20:10:20 +000038function removeArtifactsDirIf ()
david_williams6d16e662011-02-12 19:56:12 +000039{
40 # echo "arg: $1";
david_williams58f0c762011-02-12 20:32:39 +000041 foundDirectory=$1
42 if [ -z "${foundDirectory}" ]
david_williams6d16e662011-02-12 19:56:12 +000043 then
44 echo "ERROR: No argument. This function requires a directory as an argument. " ;
45 return 1;
46 fi
david_williams58f0c762011-02-12 20:32:39 +000047
david_williams6d16e662011-02-12 19:56:12 +000048 nSave=$2
49
50 if [ -z $nSave ]
51 then
52 nSave=1;
53 fi
54
david_williams58f0c762011-02-12 20:32:39 +000055 if [ ! \( -d "${foundDirectory}" \) ]
david_williams6d16e662011-02-12 19:56:12 +000056 then
57 echo "ERROR: " "${foundDirectory}" ", is not a directory. This function requires a directory as an argument. "
58 return 2;
59 fi
60
david_williamsca2f4c52011-02-12 21:24:18 +000061 # should already be in appropriate directory, if execDir in 'find' is used (and execDir, in a 'find' is recommended, as more secure)
62 # cd "${foundDirectory}"
david_williams6d16e662011-02-12 19:56:12 +000063 # move up one so we can examine syblings
david_williamsca2f4c52011-02-12 21:24:18 +000064 #cd ..
david_williams6d16e662011-02-12 19:56:12 +000065 currentDirectory=`pwd`
66 echo " current working directory: $currentDirectory";
67 ndirs=`ls -lA | wc -l`
68 ndirs=$(($ndirs - 1)); # don't count the "totals" line
69 # echo "NDirs: $ndirs"
70
71 # if only one left, do not remove it, no matter how old
72 # or, as improved ... if less than or equal to nSave is left, do not remove
73 if [ $ndirs -le $nSave ]
74 then
75 return 0;
76 fi
77 # To have no directories is unexpected, since otherwise this method should not have been called.
78 # So, this check is just a safety check.
79 if [ $ndirs -lt 1 ]
80 then
81 exit 101;
82 fi
83
david_williamsca2f4c52011-02-12 21:24:18 +000084 rm -fr "${foundDirectory}"
david_williamse2c12572011-02-12 20:53:03 +000085 rcode=$?
86 if [ $rcode -eq 0 ]
david_williams6d16e662011-02-12 19:56:12 +000087 then
david_williams417ce0a2011-02-12 21:31:03 +000088 echo " removed: " "${foundDirectory}";
david_williams6d16e662011-02-12 19:56:12 +000089 else
david_williamsca2f4c52011-02-12 21:24:18 +000090 echo "WARNING: rm returned non zero return code: " $rccode ". could not remove " "${foundDirectory}";
david_williams6d16e662011-02-12 19:56:12 +000091 fi
92
david_williamse2c12572011-02-12 20:53:03 +000093 return $rcode;
david_williams6d16e662011-02-12 19:56:12 +000094
95}