There are some rotate gz log file in the log dir,it rotaes every twenty minutes using logrotate with dateformat '.%s', as flows
ls -l /var/log/app/h323server.log.[1-9][0-9]* |head
-rw-r--r-- 1 root adm 2063852 Mar 19 02:00 /var/log/app/h323server.log.1584554401.gz
-rw-r--r-- 1 root adm 2093937 Mar 19 02:20 /var/log/app/h323server.log.1584555601.gz
I want to print the corresponding log content between start_time timestamp and end_time timestamp,there are a few steps:
1, find out the log file and fill them into an array named totalfile
2, use for loop to read totalfile and print, the first and last element need to filter by start and end timestamp, print the rest of file direcly. I want to use for (( i=1; i<${arraylength}+1; i++ ));
loop to achieve it,but something goes wrong.
The Bash script is as fllow:
#!/bin/bash
oldifs="$IFS"
IFS=$'\n'
declare -a filetime
declare -a filename
declare -a totalfile
index_1=0
index_2=0
for line in $(ls -l /var/log/app/h323server.log.[1-9][0-9]* |awk '{split($NF,a,".");print a[3],$NF}')
do
filetime[${index_1}]=$(echo ${line} |awk '{print $1}')
filename[${index_2}]=$(echo ${line} |awk '{print $2}')
((index_1++))
((index_2++))
done
IFS="$oldifs"
index=0
timesys_s=1584945601
timesys_e=1584948001
# store the corresponding delaycompress and compress file to totalfile array
while [ ${index} -le $((${#filetime[@]}-1)) ]
do
if [ ${index} -eq 0 ]
then
if [[ ${filetime[${index}]} -ge ${timesys_s} ]] || \
[[ ${filetime[${index}]} -le ${timesys_s} ]] || \
[[ (${filetime[${index}-1]} -ge ${timesys_s}) && (${filetime[${index}]} -le ${timesys_e}) ]]
then
totalfile[${index}]=${filename[${index}]}
fi
else
if [[ (${filetime[${index}-1]} -le ${timesys_s}) && (${filetime[${index}]} -ge ${timesys_s}) ]] || \
[[ (${filetime[${index}-1]} -ge ${timesys_s}) && (${filetime[${index}]} -le ${timesys_e}) ]] || \
[[ (${filetime[${index}-1]} -le ${timesys_e}) && (${filetime[${index}]} -ge ${timesys_e}) ]]
then
totalfile[${index}]=${filename[${index}]}
fi
fi
((index++))
done
echo "length of totalfile:"
echo ${#totalfile[@]}
echo "content of totalfile:"
echo ${totalfile[@]}
# get length of totalfile
arraylength=${#totalfile[@]}
# use for loop to read all values and indexes
for (( i=1; i<${arraylength}+1; i++ ));
do
echo $i " / " ${arraylength} " : " ${totalfile[$i-1]}
done
# can only print the first and last value when using ${array[index]} to loop
echo "the length of totalfile is: ${arraylength}"
echo "the 1st element: ${totalfile[0]}"
echo "the 2st element: ${totalfile[1]}"
echo "the 3st element: ${totalfile[2]}"
echo "the 4st element: ${totalfile[3]}"
echo "the 5st element: ${totalfile[-1]}"
the output is as follows:
length of totalfile:
5
content of totalfile:
/var/log/app/h323server.log.1584554401.gz /var/log/app/h323server.log.1584945601.gz /var/log/app/h323server.log.1584946801.gz /var/log/app/h323server.log.1584948001.gz /var/log/app/h323server.log.1584949201.gz
1 / 5 : /var/log/app/h323server.log.1584554401.gz
2 / 5 :
3 / 5 :
4 / 5 :
5 / 5 :
the length of totalfile is: 5
the 1st element: /var/log/app/h323server.log.1584554401.gz
the 2st element:
the 3st element:
the 4st element:
the 5st element: /var/log/app/h323server.log.1584949201.gz
The question is:
There are five element in the totalfile array, but only "${totalfile[0]}" and "${totalfile[-1]}" can print normally, while "${totalfile[1]}","${totalfile[2]}" and "${totalfile[3]}" does not print at all.
One more thing, when I use "${totalfile[-4]}","${totalfile[-3]}" and "${totalfile[-2]}", it works.
use -4,-3,-2, instead of 1,2,3
echo "the length of totalfile is: ${arraylength}"
echo "the 1st element: ${totalfile[0]}"
echo "the 2st element: ${totalfile[-4]}"
echo "the 3st element: ${totalfile[-3]}"
echo "the 4st element: ${totalfile[-2]}"
echo "the 5st element: ${totalfile[-1]}"
output:
the length of totalfile is: 5
the 1st element: /var/log/app/h323server.log.1584554401.gz
the 2st element: /var/log/app/h323server.log.1584945601.gz
the 3st element: /var/log/app/h323server.log.1584946801.gz
the 4st element: /var/log/app/h323server.log.1584948001.gz
the 5st element: /var/log/app/h323server.log.1584949201.gz
The os system is "Ubuntu 14.04.5 LTS".
I don't understand how it happens.And I'll be appreciated if anyone can explain it to me.