Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yujingz committed Aug 19, 2024
1 parent 8b2ce7d commit 13713cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
25 changes: 25 additions & 0 deletions examples/watch-and-upload/append-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# 检查参数数量
if [ "$#" -ne 2 ]; then
echo "用法: $0 <文件名> <时间间隔(秒)>"
exit 1
fi

FILE_NAME=$1
INTERVAL=$2

# 检查文件是否存在,如果不存在则创建
if [ ! -f "$FILE_NAME" ]; then
touch "$FILE_NAME"
echo "创建文件: $FILE_NAME"
fi

# 无限循环,每隔指定时间追加内容
counter=1
while true; do
echo "追加第 $counter 行内容 - $(date)" >> "$FILE_NAME"
echo "已追加第 $counter 行内容"
counter=$((counter + 1))
sleep "$INTERVAL"
done
26 changes: 3 additions & 23 deletions examples/watch-and-upload/watch-and-upload.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,12 @@ 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 "正在检查现有文件..."
search_files "$WATCH_DIR"
find "$WATCH_DIR" -type f -not -path '*/\.*' -print0 | while IFS= read -r -d '' file; do
process_file "$file"
done
echo "初始化完成。"
}

Expand Down

0 comments on commit 13713cf

Please sign in to comment.