david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # simple function that can be called via 'find' that removes and displays name of removed directory |
| 4 | function removeIf () |
| 5 | { |
| 6 | # echo "arg: $1"; |
| 7 | |
david_williams | ca2f4c5 | 2011-02-12 21:24:18 +0000 | [diff] [blame] | 8 | foundDirectory=$1 |
| 9 | if [ -z "${foundDirectory}" ] |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 10 | then |
| 11 | echo "ERROR: No argument. This function requires a directory as an argument. " ; |
| 12 | return 1; |
| 13 | fi |
david_williams | ca2f4c5 | 2011-02-12 21:24:18 +0000 | [diff] [blame] | 14 | |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 15 | |
david_williams | ca2f4c5 | 2011-02-12 21:24:18 +0000 | [diff] [blame] | 16 | if [ ! \( -d "${foundDirectory}" \) ] |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 17 | 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_williams | ca2f4c5 | 2011-02-12 21:24:18 +0000 | [diff] [blame] | 23 | #cd $foundDirectory |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 24 | |
david_williams | ca2f4c5 | 2011-02-12 21:24:18 +0000 | [diff] [blame] | 25 | rm -fr "${foundDirectory}" |
david_williams | e2c1257 | 2011-02-12 20:53:03 +0000 | [diff] [blame] | 26 | rcode=$? |
| 27 | if [ $rcode -eq 0 ] |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 28 | then |
david_williams | 54e2800 | 2011-02-12 21:34:36 +0000 | [diff] [blame] | 29 | echo " removed: " "${foundDirectory}"; |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 30 | else |
david_williams | ca2f4c5 | 2011-02-12 21:24:18 +0000 | [diff] [blame] | 31 | echo "WARNING: rm returned non zero return code: " $rccode ". could not remove " "${foundDirectory}"; |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 32 | fi |
| 33 | |
david_williams | e2c1257 | 2011-02-12 20:53:03 +0000 | [diff] [blame] | 34 | return $rcode; |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 35 | } |
| 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_williams | 1875e48 | 2011-02-12 20:10:20 +0000 | [diff] [blame] | 38 | function removeArtifactsDirIf () |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 39 | { |
| 40 | # echo "arg: $1"; |
david_williams | 58f0c76 | 2011-02-12 20:32:39 +0000 | [diff] [blame] | 41 | foundDirectory=$1 |
| 42 | if [ -z "${foundDirectory}" ] |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 43 | then |
| 44 | echo "ERROR: No argument. This function requires a directory as an argument. " ; |
| 45 | return 1; |
| 46 | fi |
david_williams | 58f0c76 | 2011-02-12 20:32:39 +0000 | [diff] [blame] | 47 | |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 48 | nSave=$2 |
| 49 | |
| 50 | if [ -z $nSave ] |
| 51 | then |
| 52 | nSave=1; |
| 53 | fi |
| 54 | |
david_williams | 58f0c76 | 2011-02-12 20:32:39 +0000 | [diff] [blame] | 55 | if [ ! \( -d "${foundDirectory}" \) ] |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 56 | then |
| 57 | echo "ERROR: " "${foundDirectory}" ", is not a directory. This function requires a directory as an argument. " |
| 58 | return 2; |
| 59 | fi |
| 60 | |
david_williams | ca2f4c5 | 2011-02-12 21:24:18 +0000 | [diff] [blame] | 61 | # 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_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 63 | # move up one so we can examine syblings |
david_williams | ca2f4c5 | 2011-02-12 21:24:18 +0000 | [diff] [blame] | 64 | #cd .. |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 65 | 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_williams | ca2f4c5 | 2011-02-12 21:24:18 +0000 | [diff] [blame] | 84 | rm -fr "${foundDirectory}" |
david_williams | e2c1257 | 2011-02-12 20:53:03 +0000 | [diff] [blame] | 85 | rcode=$? |
| 86 | if [ $rcode -eq 0 ] |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 87 | then |
david_williams | 417ce0a | 2011-02-12 21:31:03 +0000 | [diff] [blame] | 88 | echo " removed: " "${foundDirectory}"; |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 89 | else |
david_williams | ca2f4c5 | 2011-02-12 21:24:18 +0000 | [diff] [blame] | 90 | echo "WARNING: rm returned non zero return code: " $rccode ". could not remove " "${foundDirectory}"; |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 91 | fi |
| 92 | |
david_williams | e2c1257 | 2011-02-12 20:53:03 +0000 | [diff] [blame] | 93 | return $rcode; |
david_williams | 6d16e66 | 2011-02-12 19:56:12 +0000 | [diff] [blame] | 94 | |
| 95 | } |