getopt -> getopts (better builtin, because getopt-quoting ist horrible). Makefile: lmtab.example added

This commit is contained in:
Denis Knauf 2012-07-18 16:29:29 +02:00
parent fbfae1eccd
commit 23d1ce399b
5 changed files with 45 additions and 59 deletions

View file

@ -1,5 +1,5 @@
logmanager.tar: logmanager logarchiver logcompressor logcleaner Makefile
logmanager.tar: logmanager logarchiver logcompressor logcleaner Makefile lmtab.example
pax -w -s '!^!logmanager/!' -f $@ -- $^
%.tar.gz: %.tar
@ -15,15 +15,17 @@ testdir:
mkdir $@
testdir/.lmtab: testdir
echo $</ \*.log 60 > $@.tmp
echo ./ \*.log 60 > $@.tmp
mv $@.tmp $@
testdir/.prepare: testdir testdir/.lmtab
touch $</aaaaaaaaaaaaa.log
for f in `seq 1 10` ; do touch $</logfile$$f.log || exit 1 ; done
touch $</no_log
touch $</no.log.txt
touch $@
test: testdir/.prepare testdir/.lmtab
./logmanager -nc testdir/.lmtab
./logmanager -Nc testdir/.lmtab
cd testdir ; ../logmanager -nc .lmtab
cd testdir ; ../logmanager -Nc .lmtab
cd testdir ; ../logmanager -c .lmtab

View file

@ -3,7 +3,6 @@
e='*.log'
i=
n=
p=gzip
help() {
cat <<EOF
@ -17,32 +16,31 @@ EOF
exit
}
[ 0 -lt $# ] || help
x=`getopt hnie: "$@"` || exit 1
eval set -- $x
while [ 0 -lt $# ]
while getopts hnie: o
do
o=$1
shift
case "$o" in
--) break ;;
-h) help ;;
-e) e="$1" ; shift ;;
-n) n=echo ;;
-i) i=-i ;;
h) help ;;
e) e="$OPTARG" ;;
n) n=echo ;;
i) i=-i ;;
*) help ;;
esac
done
shift $(($OPTIND-1))
[ 0 -lt $# ] || help
if ! [ 0 -lt $# ]
then
echo "Error: DIR expected"
help
fi
for d
do
find "$d" -name archive -prune -o -type f \( -name "${e}" \) -exec sh -c '
find "${d}" -name archive -prune -o -type f \( -name "${e}" \) -exec sh -c '
if [ X = "X`fuser "$1" 2>/dev/null`" ]
then
a="`dirname "$1"`/archive"
'"$n"' mkdir -p -- "$a"
'"$n"' mv '$i' -- "$1" "$a"
'"${n}"' mkdir -p -- "$a"
'"${n}"' mv '$i' -- "$1" "$a"
fi ' -- '{}' \;
done

View file

@ -14,22 +14,17 @@ EOF
exit
}
[ 0 -lt $# ] || help
x=`getopt hina: "$@"` || exit 1
eval set -- $x
while [ 0 -lt $# ]
while getopts hina: o
do
o=$1
shift
case "$o" in
--) break ;;
-h) help ;;
-a) a=$1 ; shift ;;
-i) i=-i ;;
-n) n=echo ;;
-) break ;;
h) help ;;
a) a="$OPTARG" ; shift ;;
i) i=-i ;;
n) n=echo ;;
esac
done
shift $(($OPTIND-1))
[ 0 -lt $# ] || help
[ X = X"$a" ] || a="-mtime +$a"

View file

@ -10,15 +10,10 @@ EOF
exit
}
[ 0 -lt $# ] || help
x=`getopt he:ngblxc: "$@"` || exit 1
eval set -- $x
p=gzip
while [ 0 -lt $# ]
while getopts he:ngblxc: o
do
o=$1
shift
case "$o" in
--) break ;;
-h) help ;;
@ -27,9 +22,10 @@ do
-b) p=bzip2 ;;
-l) p=lzma ;;
-x) p=xz ;;
-c) p="$1" ; shift ;;
-c) p="$OPTARG" ;;
esac
done
shift $(($OPTIND-1))
[ 0 -lt $# ] || help

View file

@ -25,27 +25,23 @@ with_n() {
"$cmd" -n "$@"
}
x=`getopt hc:a:r:z:C:nN "$@"` || exit 1
eval set -- $x
while [ 0 -lt $# ]
while getopts hc:a:r:z:C:nN o
do
o=$1
shift
case "$o" in
--) break ;;
-h) help ;;
-c) c=$1 ; shift ;;
-a) archiver="$1" ; shift ;;
-r) rotator="$1" ; shift ;;
-z) compressor="$1" ; shift ;;
-C) cleaner="$1" ; shift ;;
-n) cmd=echo ;;
-N) cmd=with_n ;;
h) help ;;
c) c="$OPTARG" ;;
a) archiver="$OPTARG" ;;
r) rotator="$OPTARG" ;;
z) compressor="$OPTARG" ;;
C) cleaner="$OPTARG" ;;
n) cmd=echo ;;
N) cmd=with_n ;;
*) help ;;
esac
done
shift $(($OPTIND-1))
for n in $c ./lmtab ~/.lmtab /etc/lmtab
for n in "$c" ./lmtab ~/.lmtab /etc/lmtab
do
if [ -f "$n" ]
then
@ -60,7 +56,7 @@ then
exit 1
fi
# Format: Verzeichnis [Ausdruck[,Ausdruck[,...]] [Aufbewahrungszeit]]
# Format: Verzeichnis [Ausdruck[/Ausdruck[,...]] [Aufbewahrungszeit]]
# Verzeichnisse duerfen nur einmalig Vorkommen
sed -ne 's/#.*//;/\//p' "$c" | while read dir exp age opts
do
@ -69,8 +65,7 @@ do
do
e="`basename "${exp}"`"
exp="`dirname "${exp}"`"
[ X = "X$e" ] || e="-e $e"
$cmd $archiver "$e" "$dir"
$cmd $archiver -e "$e" "$dir"
done
$cmd $compressor "$dir"
[ 0 = "$age" ] || $cmd $cleaner "$age" "$dir"