function logez { typeset device path dir base logdir logname keep silent t status= checkerror=no for i; do shift if [[ $i = "--" ]] then break else eval "$i" fi done path=$1 (( $# > 1 )) && shift : ${keep:=30} : ${device:=file} : ${silent:=no} [[ $device = stdout ]] && return 0 [[ $device = null ]] && { exec >/dev/null 2>&1; return 0; } base=${path##*/} base=${base%.@(ksh|sh|s|ss)} [[ -z "$logdir" ]] && { if [[ $path = */* ]] then dir=${path%/*} else dir="." fi logdir=$dir/log } logdir=$logdir/$base [[ -d $logdir ]] || mkdir -p $logdir logdir=$(cd $logdir >/dev/null 2>&1 && pwd -P) while [[ -z $logname || -f $logname ]] do [[ -z $logname ]] || sleep 1 logname=$logdir/$base.log.$(date +%Y-%m-%dT%H%M%S) done touch $logname [[ $silent = "yes" ]] || printf "%s\n" "Please check the logfile: $logname" find $logdir -type f -mtime +$keep -name "$base.log.*" -exec rm {} \; >/dev/null 2>&1 if [[ $device = "both" ]]; then t=/tmp/$$.$RANDOM { $path --use-logez "$@" 2>&1; printf "%s\n" $? >$t; } | tee -a $logname status=$(< $t) while [[ -z "$status" ]]; do sleep 1 status=$(< $t) done rm $t else $path --use-logez "$@" >> $logname 2>&1 (( status = $? )) fi if [[ $checkerror = "yes" ]]; then if grep "^ERROR" $logname >/dev/null; then (( status = 1 )) fi fi return $status }