В каталоге dir есть файл rotate gz log, он вращается каждые двадцать минут, используя logrotate с dateformat '.% S' , как потоки
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
Я хочу выведите соответствующее содержимое журнала между отметкой времени start_time и отметкой времени end_time , есть несколько шагов:
1, найдите файл журнала и заполните их в массив с именем totalfile
2, используйте для l oop для чтения итогового файла и печати, элемент first и last необходимо фильтровать по начальной и конечной отметке времени, печатать остальную часть файла напрямую. Я хочу использовать for (( i=1; i<${arraylength}+1; i++ ));
l oop для достижения этой цели ,, но что-то идет не так.
Сценарий Bash выглядит следующим образом:
#!/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]}"
вывод выглядит следующим образом:
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
Вопрос в следующем:
В массиве totalfile есть пять элементов, но только "$ {totalfile [0]}" и "$ {totalfile [-1]}" может печатать нормально, в то время как «$ {totalfile [1]}», «$ {totalfile [2]}» и «$ {totalfile [3]}» не печатает вообще.
Еще одна вещь, когда я использую "$ {totalfile [-4]}", "$ {totalfile [-3]}" и "$ {totalfile [-2]}", это работает.
использование -4, - 3, -2 вместо 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]}"
вывод:
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
Система операционной системы "Ubuntu 14.04.5 LTS".
Я не понимаю, как это происходит. И я буду признателен, если кто-нибудь сможет мне это объяснить.