shell 在bash中监控目录中一定数量文件的脚本

2uluyalo  于 2022-12-30  发布在  Shell
关注(0)|答案(1)|浏览(163)

我为一个简单的剧本伤了脑筋,我需要帮助。
我有下面的场景:
我有一个目录来监视脚本将在哪里运行,直到文件数量达到预期数量(退出0)或到达到期时间(退出1)
规则:
1.命中的文件数,退出0
1.达到时间限制,即使没有达到文件量,也退出0
Usage: ./filemonitor.sh <file_mask> <file_amount> <expire_time>
Eg : ./filemonitor.sh /home/abc/ dl*.txt 5 14
我不知道我说的够不够清楚我到底做错了什么?
脚本

#!/bin/bash

# Variables
now=$(date +"%H")

# User input
files_path=$1
files_msk=$2
files_qtde=$3
files_lmttime=$4

check() {

    while :; do

        file_count=$(find $files_path -mindepth 1 -maxdepth 1 -type f -iname "$files_msk" | wc -l)

        if [ $file_count -lt $files_qtde ]; then

            if [ $now -ge $files_lmttime ]; then

                echo 'Time is over!'
                exit 1

            elif [ $now -lt $files_lmttime ]; then

                echo 'On time, process running'
                echo 'Wait 5 secs'
                sleep 5

            fi

        elif [ $file_count -eq $files_qtde ]; then

            echo "file count="$file_count "file expected="$files_qtde
            echo 'Files OK'
            exit 0

        fi

    done
}

check
fsi0uk1n

fsi0uk1n1#

问题解决了,谢谢你们.

# Check user to run
if [[ $USER != "abc" ]]; then
    echo "Abc user needed to run!"
    exit 1
fi

# Variables
now=$(date +"%H")
pause_time=2
log_path='/home/abc/logs/'
log_time=$(date +"%Y-%m-%d %H:%M:%S")
log_file=$log_path$(date +"%Y%m%d")"_filemonitor.log"

# User input
files_path=$1
files_msk=$2
files_qtde=$3
files_lmttime=$4

# Check if log path exist
if [ ! -d $log_path ]; then
    echo 'Log directory does not exist. Creating...'
    mkdir -p $log_path
fi

# Check if log file exist
if [ ! -f $log_file ]; then
    echo 'Log file does not exist. Creating...'
    touch $log_file
fi

check() {

    while :; do
        # Count how many files are in the directory
        file_count=$(ls -1 $files_path | wc -l)

        # Checks if the number of files in the directory is less than the expected amount.
        if [ $file_count -lt $files_qtde ]; then

            # Checks if the current time is less than the expiration time passed by the user
            if [ $now -gt $files_lmttime ]; then
                echo -e $log_time "Waiting for files. Wait $pause_time secs" >>$log_file
                sleep $pause_time
            else
                # Cancels the process due to time expired
                echo -e $log_time 'Time is over!' >>$log_file
                echo -e $log_time "Error" >>$log_file
                exit 1
            fi
        # Checks if the number of files equals the expected number
        elif [ $file_count -eq $files_qtde ]; then
            echo -e $log_time "Files OK" >>$log_file
            echo -e $log_time "Process OK" >>$log_file
            exit 0
        # Checks if the number of files is greater than the expected number
        elif [ $file_count -gt $files_qtde ]; then

            # Checks if the current time is less than the expiration time passed by the user
            if [ $now -gt $files_lmttime ]; then
                echo -e $log_time "Number of files larger than expected." >>$log_file
                echo -e $log_time "[$files_path] contains = "$file_count "files" >>$log_file
                echo -e $log_time "Error" >>$log_file
                exit 1
            else
                # Cancels the process due to time expired
                echo -e $log_time 'Time is over!' >>$log_file
                echo -e $log_time "Error" >>$log_file
                exit 1
            fi
        fi
    done
}

check

相关问题