当前位置: 技术问答>linux和unix
求助翻译
来源: 互联网 发布时间:2016-08-19
本文导语: #!/bin/sh logdir="/var/log" config="/var/log/rotatelogs.conf" mv="/bin/mv" default_duration=7 count=0 duration=$default_duration if [ ! -f $config ] ; then echo "$0: no config file found. Can't proceed." >&2; exit 1 fi if [ ! -w $logdir -o ! -x $logdir...
#!/bin/sh
logdir="/var/log"
config="/var/log/rotatelogs.conf"
mv="/bin/mv"
default_duration=7
count=0
duration=$default_duration
if [ ! -f $config ] ; then
echo "$0: no config file found. Can't proceed." >&2; exit 1
fi
if [ ! -w $logdir -o ! -x $logdir ] ; then
echo "$0: you don't have the appropriate permissions in $logdir" >&2
exit 1
fi
cd $logdir
# While we'd like to use ':digit:' with the find, many versions of
# find don't support POSIX character class identifiers, hence [0-9]
set -x
for name in $(find . -maxdepth 1 -type f -size +0c ! -name '*[0-9]*'
! -name '.*' ! -name '*conf' -print | sed 's/^.///')
do
set +x
count=$(( $count + 1 ))
# Grab this entry from the config file
duration="$(grep "^${name}=" $config|cut -d= -f2)"
if [ -z $duration ] ; then
duration=$default_duration
elif [ "$duration" = "0" ] ; then
echo "Duration set to zero: skipping $name"
continue
fi
back1="${name}.1"; back2="${name}.2";
back3="${name}.3"; back4="${name}.4";
# If the most recently rolled log file (back1) has been modified within
# the specific quantum, then it's not time to rotate it.
if [ -f "$back1" ] ; then
if [ -z $(find "$back1" -mtime +$duration -print 2>/dev/null) ]
then
echo -n "$name's most recent backup is more recent than $duration "
echo "days: skipping" ; continue
fi
fi
echo "Rotating log $name (using a $duration day schedule)"
set -v
# Rotate, starting with the oldest log
if [ -f "$back3" ] ; then
echo "... $back3 -> $back4" ; $mv -f "$back3" "$back4"
fi
if [ -f "$back2" ] ; then
echo "... $back2 -> $back3" ; $mv -f "$back2" "$back3"
fi
if [ -f "$back1" ] ; then
echo "... $back1 -> $back2" ; $mv -f "$back1" "$back2"
fi
if [ -f "$name" ] ; then
echo "... $name -> $back1" ; $mv -f "$name" "$back1"
fi
touch "$name"
chmod 0600 "$name"
set +v
done
if [ $count -eq 0 ] ; then
echo "Nothing to do: no log files big enough or old enough to rotate"
fi
exit 0
logdir="/var/log"
config="/var/log/rotatelogs.conf"
mv="/bin/mv"
default_duration=7
count=0
duration=$default_duration
if [ ! -f $config ] ; then
echo "$0: no config file found. Can't proceed." >&2; exit 1
fi
if [ ! -w $logdir -o ! -x $logdir ] ; then
echo "$0: you don't have the appropriate permissions in $logdir" >&2
exit 1
fi
cd $logdir
# While we'd like to use ':digit:' with the find, many versions of
# find don't support POSIX character class identifiers, hence [0-9]
set -x
for name in $(find . -maxdepth 1 -type f -size +0c ! -name '*[0-9]*'
! -name '.*' ! -name '*conf' -print | sed 's/^.///')
do
set +x
count=$(( $count + 1 ))
# Grab this entry from the config file
duration="$(grep "^${name}=" $config|cut -d= -f2)"
if [ -z $duration ] ; then
duration=$default_duration
elif [ "$duration" = "0" ] ; then
echo "Duration set to zero: skipping $name"
continue
fi
back1="${name}.1"; back2="${name}.2";
back3="${name}.3"; back4="${name}.4";
# If the most recently rolled log file (back1) has been modified within
# the specific quantum, then it's not time to rotate it.
if [ -f "$back1" ] ; then
if [ -z $(find "$back1" -mtime +$duration -print 2>/dev/null) ]
then
echo -n "$name's most recent backup is more recent than $duration "
echo "days: skipping" ; continue
fi
fi
echo "Rotating log $name (using a $duration day schedule)"
set -v
# Rotate, starting with the oldest log
if [ -f "$back3" ] ; then
echo "... $back3 -> $back4" ; $mv -f "$back3" "$back4"
fi
if [ -f "$back2" ] ; then
echo "... $back2 -> $back3" ; $mv -f "$back2" "$back3"
fi
if [ -f "$back1" ] ; then
echo "... $back1 -> $back2" ; $mv -f "$back1" "$back2"
fi
if [ -f "$name" ] ; then
echo "... $name -> $back1" ; $mv -f "$name" "$back1"
fi
touch "$name"
chmod 0600 "$name"
set +v
done
if [ $count -eq 0 ] ; then
echo "Nothing to do: no log files big enough or old enough to rotate"
fi
exit 0
|
给你大概解释了一下 由于个人水平有限 难免有错误 仅供参考
#!/bin/sh
#这一段是变量赋值,没什么好解释的吧
logdir="/var/log"
config="/var/log/rotatelogs.conf"
mv="/bin/mv"
default_duration=7
count=0
#把default_duration的值赋给duration
duration=$default_duration
#判断config文件(/var/log/rotatelogs.conf)是否不存在
#如果不存在把"$0: no config file found. Can't proceed."定向到标准错误 并以1的状态退出 异常结束
#$0 shell文件本身 >&2定向到标准错误 exit 1 以1的状态退出shell 即执行失败
if [ ! -f $config ] ; then
echo "$0: no config file found. Can't proceed." >&2; exit 1
fi
#!非 -w文件可写 -x文件可执行 对于文件夹可执行的意思是可以进入该文件夹 -o 即or 相当于别的语言的||
#那么 你可以看懂这句
if [ ! -w $logdir -o ! -x $logdir ] ; then
echo "$0: you don't have the appropriate permissions in $logdir" >&2 #解释同上面一段
exit 1 #解释见上一段
fi
#上面的判断不成立的话 程序自然顺序执行到此处
#进入 $logdir 即 /var/log
cd $logdir
# While we'd like to use ':digit:' with the find, many versions of
# find don't support POSIX character class identifiers, hence [0-9]
#set -x debug开始 set +x debug结束 所以会输出从set -x开始到set +x结束的代码执行过程的详细情况 即for的条件处理
set -x
#. 当前目录 -maxdepth 指定find命令查找时候的目录层数 -type 指定查找类型 -name 指定查找文件名 -size 指定size
# 查找当前目录下(不包含子目录) 文件size大于0 文件名不包含数字 不以.开头(即非隐藏文件) 不以conf结尾的所有文件
# sed 's/^.///' 因为是在当前目录下查找 所以找到文件后的显示格式会在每个文件名前面有./符号 所以这句sed就是去掉./
#for name 当然就是遍历查找到的文件 挨个处理
for name in $(find . -maxdepth 1 -type f -size +0c ! -name '*[0-9]*'
! -name '.*' ! -name '*conf' -print | sed 's/^.///')
do
set +x
#相当于别的语言的count=count+1
count=$(( $count + 1 ))
# Grab this entry from the config file
#把grep "^${name}=" $config|cut -d= -f2的执行结果赋给duration
#grep "^${name}=" $config 在config文件中查找以${name}=开头的行 ${name}就是循环处理中的每个元素
#cut -d= -f2 在grep的输出结果里取以=区分的第2域 即等号后的内容 赋给duration
duration="$(grep "^${name}=" $config|cut -d= -f2)"
#-z 字符串长度为0 则为真
if [ -z $duration ] ; then
#如果$duration长度为0 则把default_duration的值赋给duration
duration=$default_duration
elif [ "$duration" = "0" ] ; then
#如果$duration的值为0 则输出Duration set to zero: skipping $name
echo "Duration set to zero: skipping $name"
#和别的语言的continue一样 跳出 进入下一次循环
continue
fi
#赋值不用讲了吧 把name的值后分别添加.1 .2 .3 .4后缀 分别赋给对应的4个变量
back1="${name}.1"; back2="${name}.2";
back3="${name}.3"; back4="${name}.4";
# If the most recently rolled log file (back1) has been modified within
# the specific quantum, then it's not time to rotate it.
#-f 判断文件是否为一个普通文件(当然包括了判断文件是否存在)
if [ -f "$back1" ] ; then
#如果$back1为一个存在的普通文件 则查找duration日以前修改的名为$back1的文件 并判断查找结果是否为空
#把查找的标准错误定向到/dev/null
if [ -z $(find "$back1" -mtime +$duration -print 2>/dev/null) ]
then
#如果 查找结果为空 就输出信息 然后跳入下一次循环 -n echo的输出不换行
echo -n "$name's most recent backup is more recent than $duration "
echo "days: skipping" ; continue
fi
fi
#输出信息
echo "Rotating log $name (using a $duration day schedule)"
#-v输入行被读取时 显示shell输入行 参照上面的set +-x
set -v
# Rotate, starting with the oldest log
#开始进行log的世代更新处理
#类似 log.n->log.(n+1) ... log.1->log.2 log->log.1
if [ -f "$back3" ] ; then
#如果$back3是普通文件 则输出... $back3 -> $back4 执行$mv -f "$back3" "$back4" 这个不用解释了把
#下面的类推
echo "... $back3 -> $back4" ; $mv -f "$back3" "$back4"
fi
if [ -f "$back2" ] ; then
echo "... $back2 -> $back3" ; $mv -f "$back2" "$back3"
fi
if [ -f "$back1" ] ; then
echo "... $back1 -> $back2" ; $mv -f "$back1" "$back2"
fi
if [ -f "$name" ] ; then
echo "... $name -> $back1" ; $mv -f "$name" "$back1"
fi
#以上世代数处理后 新建$name文件
touch "$name"
#修改$name文件权限为0600
chmod 0600 "$name"
set +v
done
#至此 for循环结束
if [ $count -eq 0 ] ; then
#如果count的值等于0 则输出下面的信息
echo "Nothing to do: no log files big enough or old enough to rotate"
fi
#程序结束 以0的状态退出 即正常结束
exit 0
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。