0

My bash script is like :

vol_id=("ocid1.volume.oc1" "ocid1.volume.oc1.iad")
vol1=("volume1" "volume2")
for vol2 in "${vol1[@]}"
do
echo $vol2
for vol_nm in "${vol_id[@]}"
do
oci bv backup list -c ocid1.compartment.oc1--volume-id  $vol_nm --limit 1 --sort-by TIMECREATED > a.txt
dt=`grep -i time-created a.txt | tr -s ' ' | cut -d ':' -f2|cut -d '"' -f2`
h=`grep -i time-created a.txt | tr -s ' ' | cut -d ':' -f3`
m=`grep -i time-created a.txt | tr -s ' ' | cut -d ':' -f4|cut -d '.' -f1`
bkp_date="$dt:$h:$m"
echo $bkp_date
done
done

This script will find latest backup date for each volume id sent in vol_id array. I want output like :

Volume1 2018-07-09T05:45:45
Volume2 2018-07-09T05:11:16

But i am getting:

Volume1
2018-07-09T05:45:45
2018-07-09T05:11:16
Volume2
2018-07-09T05:45:45
2018-07-09T05:11:16
1
  • 1
    You need to be accessing the arrays by index then you need to remove the inner loop and instead have vol_nm set to the corresponding index name. See this: tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html Commented Jul 12, 2018 at 10:39

3 Answers 3

3

Try something like this:

#!/bin/bash

vol_id=("ocid1.volume.oc1" "ocid1.volume.oc1.iad")
vol1=("volume1" "volume2")

VolIndex=0
MaxIndices=${#vol_id[@]}

while (($VolIndex < $MaxIndices))
do
    echo "${vol_id[$VolIndex]} ${vol1[$VolIndex]}"
    ((++VolIndex))
    ...

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

Comments

1

The problem is that you have an inner loop that echo one element in your first array and then all the elements in your second array... Let's supose this:

vol_id=("a" "b")
vol1=("volume1" "volume2")
for vol2 in "${vol1[@]}"
do
echo $vol2
for vol_nm in "${vol_id[@]}"
do
echo $vol_nm
done
done

OUTPUT FOR THIS CODE IS:

volume1
a
b
volume2
a
b

Why? because your first loop does and echo of only one element in $vol1, then loop all elements in $vol_id, continues with the second element in $vol1 and loop all elements in $vol_id again.

What you need in this case is:

vol_id[0]-->vol1[0]
vol_id[1]-->vol1[1]
vol_id[2]-->vol1[2]
.
.    
.
vol_id[x]-->vol1[x]

How? one method is having a counter that give the same position for both arrays:

#!bin/bash
vol_$id=("a" "b")
vol1=("volume1" "volume2")
max=${#vol_id[@]};
for i in `seq 0 $((max -1))`
do
echo "${vol1[$i]} ${vol_id[$i]}"
done

I think the solution for your code is:

#!/bin/bash
vol_id=("ocid1.volume.oc1" "ocid1.volume.oc1.iad")
vol1=("volume1" "volume2")
max=${#vol_id[@]};
for i in `seq 0 $((max -1))`
   do
   oci bv backup list -c ocid1.compartment.oc1--volume-id  ${vol_id[$i]} --limit 1 --sort-by TIMECREATED > a.txt
   dt=`grep -i time-created a.txt | tr -s ' ' | cut -d ':' -f2|cut -d '"' -f2`
   h=`grep -i time-created a.txt | tr -s ' ' | cut -d ':' -f3`
   m=`grep -i time-created a.txt | tr -s ' ' | cut -d ':' -f4|cut -d '.' -f1`
   bkp_date="$dt:$h:$m"
   echo ${vol1[$i]} $bkp_date
done

Hope it helps you

Comments

-1

You can try adding the following:

head -1

5 Comments

But my code is giving duplicate result for backup date
In all seriousness Akash, I think you need to use array indexes properly for this. Remove the inner loop and use an index to access the array element.
i have 13-15 elements in my first array.
But you have the same number of disk volumes as dates. So you need to count through and pair the disk name with the date and print them together. That requires exactly 1 loop, not 2.
See my answer. You'll need to adapt it, i have to go to lunch now.

Your Answer

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