Get the output in JSON format, and then you'll be able to extract information in a more reliable way:
pv_info=$(pvs -o pv_all,vg_all --unit b --nosuffix --reportformat json)
sdb1_vg=$(
printf '%s\n' "$pv_info" |
jq -r '.report[].pv[]|select(.pv_name == "/dev/sdb1").vg_name'
)
if [ "$sdb1_vg" = vg_name ]; then...
Or use a proper programming language with a JSON library instead of a shell (ksh93
does have JSON support though in its upcoming version).
(you need LVM 2.02.158 (2016) or newer for --reportformat json
).
If it's just that one query you want to do, pvs
can also do all the work for you:
sdb1_vg=$(
pvs -o vg_name -S pv_name=/dev/sdb1 --no-heading --config 'log{prefix=""}'
)
(you need LVM 2.02.107 (2014) or newer for -S
).
Also remember to quote your variables and avoid echo
.