1
我写过一个脚本来删除远程服务器上的文件。有多个目录,并且脚本移动到每个目录并找到比定义的时间早的文件并删除它。但是如果脚本找到要删除的文件,它将删除它们并从while循环中出来。使用SSH在远程服务器上的多个目录中删除文件
输入:具有目录列表和文件名的目录列表文件。
以下是代码。
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' [email protected]$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 [email protected]$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
我希望它循环到所有目录。但当它从任何目录中找到并删除文件时,它会退出循环。
但是我的目录列表中有路径名在同一行,在接下来的后续行的下一行,并保留期限的文件名等等。每个文件的文件名和保留期都会更改。 – manjosh
然后你需要用'read'替换其他的用法。 – danny
在上面,只有在循环中使用'read'的值。 – danny