1

I have two arrays

value={00..23}
i=(01 02)

When I multiply them using the following command, I get results but I need another format ... I need the result as

00, 01...18, ...36

and now I get,

0,1 ..8, 18,..36

t=$(expr $value*$i | bc)

Anybody can show me the way?

and if I want to sum up 24 at value when I get the second element of the array days and zero if I get the first element of the array days at value...I have used the following code without success...

#!/bin/bash

data=`date +%Y-%m-%d`
data1=`date -d "1 day" +%Y-%m-%d`

days=("$data" "$data1")
for day in "${days[@]}"; do
for value in {00..23}; do
# here I order to sum up 00 if I get the first day, and if I get the second day, I order to sum up 24 at value...
if [[ "$day"="${days[0]}" ]]; then i=00
else
i=24
fi
t=$((10#$i+10#$value))
echo $t
done
done

I should get 00, 01, 02...23, 24, 25, 26...48..but i don't get it....

2
  • You have one array; value is just a space-separated string. Commented Nov 1, 2015 at 15:05
  • 1
    even worse, value is just the (verbatim) string {00..23}. Commented Nov 1, 2015 at 15:16

1 Answer 1

3

Your question isn't very clear. I guess you're in this situation:

value=( {00..23} ) # <--- this defines an array, unlike your code in the question
i=( 01 02 )

and you want to loop through the arrays value and i and multiply the terms. You don't need expr and bc for this, since you're only dealing with integers. The only catch is that is that you have a leading 0; so we have to take extra care to tell Bash we really mean numbers in radix 10 (and not in radix 8), using the radix specifier 10#. Also, we deal with your formatting problem using printf:

for a in "${i[@]}"; do
    for b in "${value[@]}"; do
        printf '%02d\n' "$((10#$a*10#$b))"
    done
done

This will print to standard out the following:

00
01
02
03
04
[...]
22
23
00
02
04
06
[...]
44
46

If you want to make a new array out of these, proceed as follows:

newarray=()
for a in "${i[@]}"; do
    for b in "${value[@]}"; do
        printf -v fab '%02d' "$((10#$a*10#$b))"
        newarray+=( "$fab" )
    done
done

Then,

declare -p newarray

will show:

declare -a newarray='([0]="00" [1]="01" [2]="02" [3]="03" [4]="04" [5]="05" [6]="06" [7]="07" [8]="08" [9]="09" [10]="10" [11]="11" [12]="12" [13]="13" [14]="14" [15]="15" [16]="16" [17]="17" [18]="18" [19]="19" [20]="20" [21]="21" [22]="22" [23]="23" [24]="00" [25]="02" [26]="04" [27]="06" [28]="08" [29]="10" [30]="12" [31]="14" [32]="16" [33]="18" [34]="20" [35]="22" [36]="24" [37]="26" [38]="28" [39]="30" [40]="32" [41]="34" [42]="36" [43]="38" [44]="40" [45]="42" [46]="44" [47]="46")'

Regarding your edit: the line

if [[ "$day"="${days[0]}" ]]; then i=00

is wrong: you need spaces around the = sign (otherwise Bash only tests whether the string obtained by the expansion "$day"="${days[0]}" is not empty… and this string is never empty!—that's because Bash only sees one argument between the [[...]]). Write this instead:

if [[ "$day" = "${days[0]}" ]]; then i=00

Now, actually, your script would be clearer (and shorter and more efficient) without the hard-coded 24, and without this test for each iteration of the loop:

#!/bin/bash

data=$(date +%Y-%m-%d)
data1=$(date -d "1 day" +%Y-%m-%d)

days=( "$data" "$data1" )
values=( {00..23} )

for day in "${days[@]}"; do
   for value in "${values[@]}"; do
      t=$((10#$i+10#$value))
      echo "$t"
   done
   ((i+=${#values[@]}))
done
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the comments. @gniourf_gniourf I have another doubt, I explain it on my question above
@EnricAgudPique: post edited to answer your question…

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.