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
すべてのディレクトリにループします。しかし、ディレクトリからファイルを見つけて削除すると、ループを終了します。
しかし、私のディレクトリリストは、1行に次の後続の行の次の行と保存期間内のファイル名をパス名を持っています等々。ファイル名と保存期間はファイルごとに変わります。 – manjosh
それから、 'read'のそれらの使い方を別のものに置き換える必要があります。 – danny
上記では、 'read'の値がループ内で使用されている場合のみです。 – danny