0

I need help with a strange problem.

This line is not creating an array list, I don't know why.

 date_final=($(echo $((date_end-date_start)) | grep -o "[0-9].*")) # PROBLEM

The Whole code is below, the particular array converts a wrong date into a new date but it should be an arraylist, and it creates a single variable instead.

#! /bin/bash

cd /var/lib/zabbixsrv/externalscripts/Manager
rm Unique.txt
declare -a date_final='()'
total_count=$(cat amazon.html 2>/dev/null | jq '.meta.total_count' | grep -o "[^\"]*")

i=0;
for i in $(seq 0 $total_count)
do
#compare=($(cat amazon.html 2>/dev/null |  jq ".objects[$i].service_tag" |  tr ' ' '\n'))
compare=($(cat amazon.html 2>/dev/null |  jq ".objects[$i].conference" | grep -o "[^\"]*" |  tr ' ' '\n'))
echo -e ${compare[@]}  >> /var/lib/zabbixsrv/externalscripts/Manager/Unique.txt
done

compare1=($(cat Unique.txt | uniq -c | gawk '$1==1{print $2}'))

Number_line=$(echo ${#compare1[@]}) # PROBLEMA RESOLVIDO!!!!

let Number_line-=1


#echo -e ${compare[@]}

for i in $(seq 0 $Number_line)
do

#time=$(cat amazon.html 2>/dev/null | jq  '.objects[] | select(.service_tag=='${compare1[$i]}')' | jq  ".connect_time" |  grep -o "[^\"]*" | grep -o "[^T][0-9].*" | grep -o "[0-9]\{2\}:[0-9]\{2\}:[0-9].")
time=$(cat amazon.html 2>/dev/null | jq  '.objects[] | select(.conference=='${compare1[$i]}')' 2>/dev/null | jq  ".connect_time" |  grep -o "[^\"]*" | grep -o "[^T][0-9].*" | grep -o "[0-9]\{2\}:[0-9]\{2\}:[0-9].")
#echo -e  ${compare1[$i]}
date_convert=$(date -d "$time 2 hour ago" +"%H:%M:%S")
date_start=$(date -d "$day $date_convert" +%s)
date_end=$(date +"%s")
date_final=($(echo $((date_end-date_start)) | grep -o "[0-9].*")) # PROBLEM
done
# rm Tenant.txt
#echo ${date_final[0]}
#echo -e ${date_final[@]}

tempo=1
i=0
echo -e  $Numero_linha
echo -e ${date_final[@]}


for i in $(seq 0 $Number_line)
do

if ((${date_final[$i]} > $tempo)) 2>/dev/null; then
        echo -e  ${compare1[$i]}
fi
done

Thanks Very Much

1 Answer 1

2

I have used the code from your example to declare date_final and initialize date_start and date_end, and I actually get an array after executing your problematic statement.

I think your problem is you are repeatedly assigning a one-element array to your variable instead of adding elements.

Try this :

date_final+=( $((date_end-date_start)) )

The key here is usine the += operator, which appends instead of assigning. The rest is just cleanup, as you do not need grep to filter for digits (there are only digits in the result of the calculation), and the echo is not required either.

Sign up to request clarification or add additional context in comments.

4 Comments

declare -a date_final='()' Yes I tried everything, no the statement doesn't produce any erros at all. In echo -e ${date_final[@]}, just the fisrt element shows, and there are more than 10 of them.
There cannot be more than one element. When you perform the $((date_end-date_start)) calculation, it produces one string of digits only. Then you feed that into "grep", which probably does nothing because there were only digits to start with in your result. This is then turned into a one-element array.
I think I understand your problem. The absence of indentation made me miss this was inside a loop. Please see my updated answer, coming up in a few minutes.
That is Quite Interesting, I'll Try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.