【发布时间】:2017-05-15 10:47:03
【问题描述】:
我编写了一个脚本来删除远程服务器上的文件。有多个目录,脚本移动到每个目录并找到比定义时间更旧的文件并将其删除。但是如果脚本找到要删除的文件,它会删除它们并退出 while 循环。
输入:目录列表文件,包含目录列表和文件名。
下面是代码。
MsgLog " Step 1: Start checking directories for old logs"
cat $DIRECTORY_LIST | while read DIR;
do
read LOG_FILE
read LOG_RET_PERIOD
MsgLog "Inputs"
MsgLog "Server=$SERVER User=$USER Log_path=$DIR Log_file=$LOG_FILE Log_Retention_Period=$LOG_RET_PERIOD"
ssh -q -n -o 'BatchMode yes' $USER@$SERVER "cd $DIR;find $LOG_FILE* -mtime +$LOG_RET_PERIOD" >>$FIND_log
ERROR_MSG=$?
if [ $ERROR_MSG -ne 0 ]
then
MsgLog "ERROR $ERROR_MSG: Could not connect."
exit 1
fi
if [ -s "$FIND_log" ]
then
MsgLog " Files to delete:"
cat $FIND_log
ssh $USER@$SERVER "cd $DIR;find $LOG_FILE* -mtime +$LOG_RET_PERIOD -exec rm {} \;"
ERROR_MSG=$?
if [ $ERROR_MSG -ne 0 ]
then
MsgLog "ERROR $ERROR_MSG: Step 1, Could not delete old Log Files."
exit 1
fi
else
MsgLog " No log files to delete."
fi
rm $FIND_log
MsgLog "Move to next directory"
done
MsgLog "No more directories"
if [ "$EXIT_CODE" = 0 ]
then
MsgLog "$BNAME successfully completed"
else
MsgLog "$BNAME completed with errors"
fi
exit $EXIT_CODE
我希望它循环到所有目录。但是当它从任何目录中找到并删除文件时,它会退出循环。
【问题讨论】: