user1607856
user1607856

Reputation: 79

bash Assign Result Query to Variable

The situation, where I have unknown number of volume groups and their names with unknown number of disks assigned to them. Example :

pvs -o pv_name,vg_name
  PV         VG
  /dev/vdd   appvg01
  /dev/vdb   appvg01
  /dev/vdf3  vg00
  /dev/vdh   testvg

 vgs --noheadings | awk '{print $1}'| while read line ; do echo $line;vgs --noheadings -o pv_name $line; done
appvg01
  /dev/vdd
  /dev/vdb
testvg
  /dev/vdh
vg00
  /dev/vdf3

At the final stage I'd like to mirror each volume with new disk that I'll add manually :

for i in `/sbin/lvs| /bin/awk '{if ($2 ~ /appvg01/) print $1}'`; do
  /sbin/lvconvert -b -m0 appvg01/$i /dev/vde
done

but, I don't know what volume name should I correlate with, as it might be any other name. what is the best approach for this structure.

Thanks

Upvotes: 0

Views: 58

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295315

The correct data structure to store this kind of information in bash is associative arrays:

declare -A pvs
{
  read # skip the header
  while read -r pv vg; do
     pvs[$pv]=$vg
  done
} < <(pvs -o pv_name,vg_name)

Thereafter, you can iterate and do lookups:

for pv in "${!pvs[@]}"; do
  vg="${pvs[$pv]}"
  echo "vg $vg is backed by pv $pv"
done

Upvotes: 2

Related Questions