我已经这样做了,但是我在写的shellscript上遇到了麻烦。我与尾部命令功能以及当我看到错误的输出时都感到困惑。终端上的log显示在单词中删除的“ e”。

我已经写过这样的书,请指导我如何解决问题。我想读取此错误。按行读取文件文件,在阅读行期间,我想将固定的线数分配给带有后缀IE log-aa,log-ab的小文件,...我使用split command进行了此操作。拆分后,我想使用Regex用get或port Word过滤线,然后将此过滤的线存储到新文件中。该商店完成后,我需要删除所有这些日志*文件。

我这样写了:

enter code here
processLine(){
  line="$@"
  echo $line
  $ tail -f $FILE
}
FILE="/var/log/apache2/error.log"
if [ "$1" == "/var/log/apache2/error.log" ]; then
   FILE="/dev/stdin"
else
   FILE="$1"
   if [ ! -f $FILE ]; then
   echo "$FILE : does not exists"
   exit 1
   elif [ ! -r $FILE ]; then
   echo "$FILE: can not read"
   exit 2
   fi
fi
#BAKIFS=$IFS
#IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE"
#sed -e 's/\[debug\].*\(data\-HEAP\)\:\/-->/g' error.log > /var/log/apache2/error.log.1
while read -r line
do
 processLine $line
done
exec 0<&3
IFS=$BAKIFS
logfile="/var/log/apache2/error.log"
pattern="bytes"
# read each new line as it gets written
# to the log file
#tail -1 $logfile 
tail -fn0 $logfile | while read line ; do
# check each line against our pattern
echo "$line" | grep -i "$pattern" 
#sed -e 's/\[debug\].*\(data\-HEAP\)\:/-->/g' error.log >/var/log/apache2/error.log
split -l 1000 error.log log-  
FILE2="/var/log/apache2/log-*"
if [ "$1" == "/var/log/apache2/log-*" ]; then
   FILE2="/dev/stdin"
else
   FILE2="$1"
   if [ ! -f $FILE2 ]; then
   echo "$FILE : does not exists"
   exit 1
   elif [ ! -r $FILE2 ]; then
   echo "$FILE: can not read"
   exit 2
   fi
fi
BAKIFS=$IFS
IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE2"
while read -r line
do
 processLine $line
 echo $line >>/var/log/apache2/url.txt
done
exec 0<&3
IFS=$BAKIFS
find . -name "var/log/apache2/logs/log-*.*" -delete
done
exit 0

下面的代码在阅读和拆分错误后删除文件。log,但是当我放置tail -f $文件时,它停止删除文件,我想删除log-*文件到达最后一行的错误。log文件:log文件:在此处输入代码

processLine(){
line="$@" 
echo $line
}
FILE=""
if [ "$1" == "" ]; then
FILE="/dev/stdin"
else
FILE="$1"
# make sure file exist and readable
if [ ! -f $FILE ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not read"
exit 2                                     
fi
fi
#BAKIFS=$IFS
#IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE"
while read -r line
do
processLine $line
split -l 1000 error.log log-
cat log-?? | grep "GET\|POST" > storefile 
#tail -f $FILE
done 
rm log-??
exec 0<&3 
#IFS=$BAKIFS
exit 0
有帮助吗?

解决方案

您的代码似乎不必要地漫长而复杂,逻辑尚不清楚。如果不正确工作,就不应允许它成长。

考虑一下:

split -l 1000 error.log log-
cat log-?? | grep "GET\|POST" > storefile
rm log-??

实验。如果这三个命令按照您的期望,则可以添加更多功能(例如,使用路径,检查是否存在 error.log),但是在您有效之前,不要添加代码。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top