bash脚本来查看文件夹

pra*_*bhu 4 linux bash

我有以下情况:

有一个已安装在Linux机器上的Windows文件夹.在这个Windows挂载中可能有多个文件夹(手动设置).我必须做一些事情(最好是一个开头的脚本)来观看这些文件夹.

以下是步骤:监视任何传入的文件.确保它们完全转移.将其移动到另一个文件夹.我对Windows机器上的文件传输程序没有任何控制权.我相信这是一个安全的FTP.所以我不能要求该过程向我发送预告片文件以确保完成文件传输.

我写了一个bash脚本.我想知道这种方法可能存在的任何陷阱.原因是,有可能为这样的多个目录运行此脚本的多个副本.

目前,可能需要监控多达100个目录.

以下是脚本.我很抱歉在这里贴了很长一段时间.请花点时间仔细阅读并评论/批评它.:-)

它需要3个参数,必须要监视的文件夹,文件必须移动的文件夹,以及时间间隔,这已在下面说明.

对不起,对齐似乎有问题.Markdown似乎不喜欢它.我试图正确组织它,但不能这样做.

Linux servername 2.6.9-42.ELsmp #1 SMP Wed Jul 12 23:27:17 EDT 2006 i686 i686 i386 GNU/Linux

#!/bin/bash
log_this()
{
    message="$1"
    now=`date "+%D-%T"`
    echo $$": "$now ": " $message
}
usage()
{
    cat << EOF
Usage: $0 <Directory to be watched> <Directory to transfer> <time interval>
Time interval is the amount of time after which the modification time of a
file will be monitored. 
EOF
    `exit 1`
}

if [ $# -lt 2 ]
then
    usage
fi

WATCH_DIR=$1
APP_DIR=$2

if [ ! -d "$WATCH_DIR" ]
then
    log_this "FATAL: WATCH_DIR, $WATCH_DIR does not exist. Exiting"
    exit 1
fi

if [ ! -d "$APP_DIR" ]
then
    log_this "APP_DIR: $APP_DIR does not exist. Exiting"
    exit 1
fi


# This needs to be set after considering the rate of file transfer.
# Represents the seconds elapsed after the last modification to the file.
# If not supplied as parameter, defaults to 3.

seconds_between_mods=$3

if ! [[ "$seconds_between_mods" =~ ^[0-9]+$ ]]; then
        if [ ${#seconds_between_mods} -eq 0 ]; then
                log_this "No value supplied for elapse time. Defaulting to 3."
                seconds_between_mods=3
        else
                log_this "Invalid value provided for elapse time"
                exit 1
        fi
fi

log_this "Start Monitor."

while true
do
        ls -1 $WATCH_DIR | while read file_name
        do
            log_this "Start Monitoring for $file_name"

            # Refer only the modification with reference to the mount folder.
            # If there is a diff in time between servers, we are in trouble.

            token_file=$WATCH_DIR/foo.$$
            current_time=`touch $token_file && stat -c "%Y" $token_file`
            rm -f $token_file 2>/dev/null

            log_this "Current Time: $current_time"
            last_mod_time=`stat -c "%Y" $WATCH_DIR/$file_name`

            elapsed_time=`expr $current_time - $last_mod_time`
            log_this "Elapsed time ==> $elapsed_time"

            if [ $elapsed_time -ge $seconds_between_mods ]
            then
                    log_this "Moving $file_name to $APP_DIR"

                    # In case if there is no space left on the target mount, hide the     file
                    # in the mount itself and remove the incomplete file from APP_DIR.
                    mv $WATCH_DIR/$file_name $APP_DIR
                    if [ $? -ne 0 ]
                    then
                            log_this "FATAL: mv failed!! Hiding $file_name"
                            rm $APP_DIR/$file_name
                            mv $WATCH_DIR/$file_name $WATCH_DIR/.$file_name
                            log_this "Removed $APP_DIR/$file_name. Look for $WATCH_DIR/.$file_name and submit later."
                    fi

                    log_this "End Monitoring for $file_name"
            else
                    log_this "$file_name: Transfer seems to be in progress"
            fi
    done
    log_this "Nothing more to monitor."
    echo
    sleep 5
done
Run Code Online (Sandbox Code Playgroud)

Aar*_*lla 5

这在任何时间都不会起作用.在生产中,您将遇到网络问题和其他错误,这些错误可能会在上载目录中留下部分文件.我也不喜欢"预告片"文件的想法.通常的方法是以临时名称上传文件,然后在上载完成后重命名.

这样,您只需列出目录,过滤临时名称,如果还有任何内容,请使用它.

如果您无法进行此更改,请向您的老板寻求书面许可,以实施可能导致任意数据损坏的内容.这有两个目的:1)让他们明白这是一个真正的问题而不是你构成的东西,2)在它破裂时保护自己......因为它会猜测谁会得到所有的责任?