|
@@ -1,17 +1,30 @@
|
|
|
#!/bin/bash
|
|
|
|
|
|
-# 进入指定目录
|
|
|
-cd "/logs/$1"
|
|
|
+# $1:文件夹名称
|
|
|
+# $2:匹配字符串
|
|
|
|
|
|
-# 获取最新修改的文件
|
|
|
-latest_file=$(ls -t | head -n1)
|
|
|
+dir="/logs/$1"
|
|
|
|
|
|
-# 查找入参2所在行
|
|
|
-matching_line=$(grep -n "$2" "$latest_file" | head -n1)
|
|
|
+if [ ! -d "$dir" ]; then
|
|
|
+ echo "目录 $dir 不存在"
|
|
|
+ exit 1
|
|
|
+fi
|
|
|
+
|
|
|
+file=$(ls -t "$dir" | head -n 1)
|
|
|
+
|
|
|
+if [ -z "$file" ]; then
|
|
|
+ echo "目录 $dir 中没有任何文件"
|
|
|
+ exit 1
|
|
|
+fi
|
|
|
+
|
|
|
+matching_line=$(grep -n "$2" "$dir/$file" | tail -n 1)
|
|
|
+
|
|
|
+if [ -z "$matching_line" ]; then
|
|
|
+ echo "文件 $file 中没有找到字符串 '$2'"
|
|
|
+ exit 1
|
|
|
+fi
|
|
|
|
|
|
-# 获取匹配行号和文件名
|
|
|
line_number=$(echo "$matching_line" | cut -d ":" -f 1)
|
|
|
-# file_name=$(echo "$matching_line" | cut -d ":" -f 2)
|
|
|
+echo "在文件 $file 中找到了字符串 '$2',位于第 $line_number 行:"
|
|
|
|
|
|
-# 查找前30行和后200行数据
|
|
|
-awk -v start=$((line_number-30)) -v end=$((line_number+200)) 'NR>=start && NR<=end' "$latest_file"
|
|
|
+sed -n "$((line_number - 30)),$((line_number + 200))p" "$dir/$file"
|