diff --git a/examples/watch-and-upload/watch-and-upload.sh b/examples/watch-and-upload/watch-and-upload.sh index ce96f3d..831e161 100755 --- a/examples/watch-and-upload/watch-and-upload.sh +++ b/examples/watch-and-upload/watch-and-upload.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash +trap 'echo "正在退出脚本..."; exit' INT TERM set -o errexit set -o nounset set -o pipefail -trap 'echo "正在退出..."; exit' INT if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace fi @@ -102,12 +102,32 @@ process_file() { fi } +function search_files() { + local dir="$1" + + # 遍历目录中的所有文件和子文件夹 + for file in "$dir"/*; do + # 跳过当前目录 . 和上级目录 .. + if [ "$file" == "$dir/." ] || [ "$file" == "$dir/.." ]; then + continue + fi + + if [ -d "$file" ]; then + # 如果是目录,则递归调用 search_files + search_files "$file" + elif [ -f "$file" ]; then + # 如果是文件,检查后缀 + if [[ "$file" == *".log" ]]; then + process_file "$file" + fi + fi + done +} + # 对于已有文件也做检查 initialize() { echo "正在检查现有文件..." - find "$WATCH_DIR" -type f -not -path '*/\.*' -print0 | while IFS= read -r -d '' file; do - process_file "$file" - done + search_files "$WATCH_DIR" echo "初始化完成。" } @@ -116,7 +136,7 @@ main() { initialize echo "开始监控目录: $WATCH_DIR" - fswatch --latency=5.0 --event Created --event Updated --event MovedTo -0 -r \ + fswatch --event Created --event Updated --event MovedTo -0 -r \ -e "(/|^)\.[^/]*$" \ -e "/sed.*\.tmp$" \ "$WATCH_DIR" | while read -d "" event; do