chiark
/
gitweb
/
~ian
/
chiark-utils.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
003f19f
)
nicer arg parsing
author
ianmdlvl
<ianmdlvl>
Sun, 13 Aug 2006 16:00:25 +0000
(16:00 +0000)
committer
ianmdlvl
<ianmdlvl>
Sun, 13 Aug 2006 16:00:25 +0000
(16:00 +0000)
scripts/expire-iso8601
patch
|
blob
|
history
diff --git
a/scripts/expire-iso8601
b/scripts/expire-iso8601
index 082d33a75f61a74a8023a15bd03d50e69db214e6..7cb274e607991354cf88b890b108054c5c9ed9c7 100755
(executable)
--- a/
scripts/expire-iso8601
+++ b/
scripts/expire-iso8601
@@
-3,21
+3,23
@@
set -e
usage () {
cat <<END
usage:
usage () {
cat <<END
usage:
- expire-iso8601 [<options>] <unit-in-seconds> <slop-in-seconds>
- <min-interval-in-units> <number-to-keep>
- [<min-interval-in-units> <number-to-keep> ...]
+ expire-iso8601 [<options>] <number>x<interval> [<number>x<interval> ...]
options:
options:
- -n do not really delete
- -r recursive removal (rm -r)
+ -u<unitlen> <interval> is measured in units of <unitlen> seconds
+ (default is 86400, so <interval> is in days)
+ -s<slop> allow kept items to be <slop> seconds shorter apart than
+ specified; default is 10% of <unitlen>
+ -n do not really delete
+ -r recursive removal (rm -r)
example:
example:
- /home/ian/junk/expire-iso8601
86400 10000 1 14 7 4
- uses units of 86400s (1 day) with a slop of
10ks;
+ /home/ian/junk/expire-iso8601
14x1 4x7
+ uses units of 86400s (1 day) with a slop of
8640
it keeps 14 daily items
it keeps 14 daily items
- (that is 14 items, dated no less than 86400-
10000s
apart)
+ (that is 14 items, dated no less than 86400-
8640
apart)
and 7 weekly items
and 7 weekly items
- (that is 7 items, dated no less than 7*86400-
10000s
apart)
+ (that is 7 items, dated no less than 7*86400-
8640
apart)
the 14 daily and 7 weekly items may be the same, or not
the 14 daily and 7 weekly items may be the same, or not
- There is no need to sort the list of
interval/number
pairs.
+ There is no need to sort the list of
<number>x<interval>
pairs.
exit status:
0 ok
4 rm failed
exit status:
0 ok
4 rm failed
@@
-31,7
+33,18
@@
badusage () { echo >&2 "bad usage: $*"; usage >&2; trap '' 0; exit 8; }
#-------------------- argument parsing --------------------
#-------------------- argument parsing --------------------
+alldigits () {
+ [ "x${2##*[^0-9]}" = "x$2" ] || \
+ badusage "bad $1 \`$2'; must be all digits"
+ [ "$2" ] || badusage "bad $2; must be nonempty"
+ eval $1='$2'
+}
+
rm=rm
rm=rm
+recurse=''
+unit=86400
+slop=''
+
while [ $# -ge 1 ]; do
arg=$1; shift
case "$arg" in
while [ $# -ge 1 ]; do
arg=$1; shift
case "$arg" in
@@
-39,9
+52,12
@@
while [ $# -ge 1 ]; do
--help) usage; exit 0 ;;
--*) badusage "unknown option $arg" ;;
-*)
--help) usage; exit 0 ;;
--*) badusage "unknown option $arg" ;;
-*)
+ val=${arg#-?}
case "$arg" in
-n*) rm=: ;;
-r*) recurse=-r ;;
case "$arg" in
-n*) rm=: ;;
-r*) recurse=-r ;;
+ -u*) alldigits unit "$val"; arg='' ;;
+ -s*) alldigits slop "$val"; arg='' ;;
*) badusage "unknown option ${1:0:2}" ;;
esac
arg=-${arg#-?}
*) badusage "unknown option ${1:0:2}" ;;
esac
arg=-${arg#-?}
@@
-51,24
+67,13
@@
while [ $# -ge 1 ]; do
esac
done
esac
done
-[ $# -ge 4 ] || badusage 'too few arguments'
-
-unit=$1
-slop=$2
-shift;shift
-
-[ $(($# % 2)) = 0 ] || badusage 'odd keep arguments (need min/extent pairs)'
-argl="$*"
-
-alldigits () {
- [ "x${1##*[^0-9]}" = "x$1" ] || badusage "$2 must be all digits"
- [ x$1 ] || badusage "$2 must be nonempty"
-}
+[ $# -ge 1 ] || badusage 'too few arguments'
+[ "$slop" ] || slop=$(( $unit / 10 ))
-
while [ $# -gt 0 ]
; do
- min=$1; shift; extent=$1; shift
- alldigits
$min min
- alldigits
$extent extent
+
for ni in "$@"
; do
+ case "$ni" in *x*);; *) badusage "bad <number>x<interval> $ni";; esac
+ alldigits
number "${ni%%x*}"
+ alldigits
interval "${ni#*x}"
done
#-------------------- scanning the directory ----------
done
#-------------------- scanning the directory ----------
@@
-117,10
+122,9
@@
$l
END
)
END
)
-set $argl
-while [ $# != 0 ]; do
- min=$(( $1 * $unit - $slop ))
- wantcount=$2
+for ni in "$@"; do
+ min=$(( ${ni#*x} * $unit - $slop ))
+ wantcount=${ni%x*}
ls=''
lnew=''
ls=''
lnew=''
@@
-129,7
+133,7
@@
while [ $# != 0 ]; do
if [ $wantcount != 0 ]; then
if ! [ "$ls" ] || \
[ $(( $ls - $cs )) -ge $min ]; then
if [ $wantcount != 0 ]; then
if ! [ "$ls" ] || \
[ $(( $ls - $cs )) -ge $min ]; then
- echo "keep (for $
1 $2
) $cn"
+ echo "keep (for $
ni
) $cn"
ls=$cs
ce=:$cs/$cn
wantcount=$(( $wantcount - 1 ))
ls=$cs
ce=:$cs/$cn
wantcount=$(( $wantcount - 1 ))
@@
-138,9
+142,8
@@
while [ $# != 0 ]; do
lnew="$lnew $ce"
done
if [ $wantcount != 0 ];then
lnew="$lnew $ce"
done
if [ $wantcount != 0 ];then
- echo "insufficient (for $
1 $2
) by $wantcount"
+ echo "insufficient (for $
ni
) by $wantcount"
fi
fi
- shift;shift
l=$lnew
done
l=$lnew
done
@@
-161,7
+164,7
@@
for ce in $l; do
*)
cn=${ce#*/}
nonbroken_echo "expire $cn"
*)
cn=${ce#*/}
nonbroken_echo "expire $cn"
-
echo
$rm $recurse -- $cn &
+ $rm $recurse -- $cn &
jobs="$jobs $!"
;;
esac
jobs="$jobs $!"
;;
esac