Skip to content

Commit

Permalink
put back log logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yujingz committed Aug 20, 2024
1 parent 264abae commit 8f28ffc
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions examples/watch-and-upload/watch-and-upload.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 "初始化完成。"
}

Expand All @@ -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
Expand Down

0 comments on commit 8f28ffc

Please sign in to comment.