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

View file

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

View file

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

View file

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

View file

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