6

というディレクトリがあり/home/user/localます。約 2 分ごとに、新しいファイルがこのディレクトリにダンプされます。このディレクトリを 2 分ごとにチェックして、新しいファイルがそこに到着したかどうかを確認する必要があります。新しいファイルがある場合は、後で使用するために、そのリストを変数に入れる必要があります。このシェルスクリプトを実行するにはどうすればよいですか?

4

5 に答える 5

6
#! /usr/bin/env bash

FILELIST=/tmp/filelist
MONITOR_DIR=/home/usr/local

[[ -f ${FILELIST} ]] || ls ${MONITOR_DIR} > ${FILELIST}

while : ; do
    cur_files=$(ls ${MONITOR_DIR})
    diff <(cat ${FILELIST}) <(echo $cur_files) || \
         { echo "Alert: ${MONITOR_DIR} changed" ;
           # Overwrite file list with the new one.
           echo $cur_files > ${FILELIST} ;
         }

    echo "Waiting for changes."
    sleep $(expr 60 \* 2)
done

迅速で汚い方法。:)新しいファイルがダンプされたときだけでなく、ファイルが見つからない/削除された場合にも、ディレクトリの変更を監視します。

ファイルリストは変数に格納されます$cur_files

于 2012-11-07T08:29:13.750 に答える
2

inotifywaitまさにあなたが探しているものです: http://linux.die.net/man/1/inotifywait

于 2012-11-07T06:51:18.617 に答える
0

まず、Leafei さんに感謝します。ただし、ループ用の一時ファイルを作成するデバイスでは、現在のプロジェクトでは移植性が十分ではなく、監視対象のディレクトリ内で発生する可能性のあるさまざまなことに対してアクションを実行する必要があったため、leafei の回答に基づいて作成し、ローカルでテストしたスクリプトを次に示します。最初の投稿でフォーマットがオフになっている可能性があることに注意してください...

#!/usr/bin/env bash
Var_monitor_dir="${1?No directory passed}"
Var_sleep_time="${2:-120}"
Var_diff_opts="--suppress-common-lines"
Func_parse_change(){
    _added_input="$(grep -E ">" <<<"${@}")"
    _removed_input="$(grep -E "<" <<<"${@}")"
    if [ "${#_added_input}" != "0" ]; then
            mapfile -t _arr_added <<<"${_added_input}"
            let _added_count=0
            until [ "${#_arr_removed}" = "${_added_count}" ]; do
                _current_path="${_arr_removed[${_added_count}]}"
                if [ -f "${_current_path}" ]; then
                    echo "# ${0##*/} detected added file: ${_current_path}"
                elif [ -d "${_current_path}" ]; then
                    echo "# ${0##*/} detected added directory ${_current_path}"
                elif [ -p "${_current_path}" ]; then
                    echo "# ${0##*/} detected added named pipe ${_current_path}"
                fi
                ## Ignore other added input and add to index counter
                let _added_count++
            done
            unset _added_count
    fi
    if [ "${#_removed_input}" != "0" ]; then
            mapfile -t _arr_removed <<<"${_added_input}"
            let _removal_count=0
            until [ "${#_arr_removed}" = "${_removal_count}" ]; do
                echo "# Removal detected: ${_arr_removed[${_removal_count}]}"
                let _removal_count++
            done
    fi
 }
 Func_watch_dir(){
    _current_listing=""
    while [ -d "${Var_monitor_dir}" ]; then
         _new_listing="$(ls "${Var_monitor_dir}")"
         _diff_listing="$(diff ${Var_diff_opts} <(echo "${_current_listing}") <(echo "${_new_listing}"))"
        if [ "${#_diff_listing}}" != "0" ]; then
            Func_parse_change "${_diff_listing}"
        fi
        _current_listing="${_new_listing}"
        sleep ${Var_sleep_time}
    done
}
if [ "${#@}" != "0" ]; then
     Func_watch_dir
     exit 0
else
     echo "${0##*/} needs a directory to monitor"
     exit 1
if

上記が as として保存されmonitor_directory.sh、監視対象のディレクトリが/tmp1 つのターミナルを開いて入力するとmonitor_directory "/tmp"(またはmonitor_directory.sh "/tmp" "10"テスト間の更新を高速化するために)、変更をチェックするループが開始されます。次に、2 番目の端末を開いて、次のように入力し、最初の端末が更新されるのを待ちます。

mkdir -p /tmp/test_dir
touch /tmp/test.file

待機時間が経過すると、最初の端末はファイルとディレクトリが表示されたことを報告するはずです。次を実行して再度待機すると、削除も表示されます。

rmdir /tmp/test_dir
rm /tmp/test.file

これが役立つことを願っています。

于 2016-11-25T11:49:27.563 に答える
0

あなたの変数が $listOF と呼ばれているとしましょう

スクリプトを作成します。

listOfFiles=`ls -t /home/user/local`
for i in $listOfFiles
do
echo "$listOF" | grep $i
if [[ "$?" -eq "0" ]]
then
listOF=$listOF" "$i
fi
done

そして、このスクリプトを cmd: crontab -e の形式で crontab に入れます:

1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59 * * * * <path to your script>
于 2012-11-07T08:30:09.717 に答える
0

現在のリストを取得し、それをファイルに保存されている古いリストと比較するスクリプトを実行する cron ジョブを設定します。

于 2012-11-07T06:51:41.127 に答える