'Unable to access variable value inside nested if statement in bash
I am trying to write a bash script that takes advantage of libvirt hooks to set the vlan on the vm is connected to (using openvswitch). The challenge however is using the portAssigned variable inside the nested if statement, everything I've tried so far has failed. I can see the variable outside the if statement but within it, it is empty, however the portNumber variable works fine, how do I pass in $portAssigned variable to the inside if statement?
$ bash --version
GNU bash, version 5.1.8(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
#!/bin/bash
portNumber=$(cut -d "_" -f2 <<< "$1")
domainXML=`cat /dev/stdin`
portAssigned=`echo "$domainXML" | xmllint --xpath 'string(//domain/devices/interface[@type="bridge"]/target/@dev)' -`
DATE=$(date)
echo $domainXML | tee /home/generic_vm/domainXML.txt
echo $portAssigned | tee -a /home/generic_vm/domainXML.txt
if [ "$1" = "gene01_16" ]; then
if [ "$2" = "prepare" ]; then
echo "True" > /home/generic_vm/libvirt_hook.txt
ovs-vsctl set port $portAssigned tag=$portNumber
echo "Creating port in Openvswitch bridge for vm: $1, tag: ${portNumber}, portname: $portAssigned at ${DATE}" | tee -a /home/generic_vm/libvirt_hook.txt
fi
elif [ "$1" = "gene01_16" ]; then
if [ "$2" = "release" ]; then
ovs-vsctl del-port br0 $portAssigned
echo "Deleting port in Openvswitch bridge for vm: ${vm}, portname: $portAssigned at ${DATE}" | tee -a /home/generic_vm/libvirt_hook.txt
fi
fi
$ cat domainXML.txt
<domain type='kvm' id='88'> <name>gene01_16</name> <uuid>d1876d79-7fea-42b1-bd27-62a71809afaa</uuid> <memory unit='KiB'>524288</memory> <currentMemory unit='KiB'>524288</currentMemory> <vcpu placement='static'>1</vcpu> <resource> <partition>/machine</partition> </resource> <os> <type arch='x86_64' machine='pc-i440fx-impish'>hvm</type> <boot dev='hd'/> </os> <features> <acpi/> <apic/> <pae/> </features> <cpu mode='host-passthrough' check='none' migratable='on'/> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <devices> <emulator>/usr/bin/qemu-system-x86_64</emulator> <disk type='file' device='cdrom'> <driver name='qemu' type='raw'/> <source file='/mnt/sdadisk/default/commoninit.iso' index='1'/> <backingStore/> <target dev='hdd' bus='ide'/> <readonly/> <alias name='ide0-1-1'/> <address type='drive' controller='0' bus='1' target='0' unit='1'/> </disk> <controller type='usb' index='0' model='piix3-uhci'> <alias name='usb'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/> </controller> <controller type='pci' index='0' model='pci-root'> <alias name='pci.0'/> </controller> <controller type='ide' index='0'> <alias name='ide'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> </controller> <controller type='virtio-serial' index='0'> <alias name='virtio-serial0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </controller> <interface type='bridge'> <mac address='52:54:00:d2:2a:3a'/> <source network='br0' portid='acafb864-bcdb-4151-a3c7-40cb775f3a97' bridge='br0'/> <virtualport type='openvswitch'> <parameters interfaceid='bc652b84-773f-4924-a231-7151c01a9651'/> </virtualport> <target dev='vnet48'/> <model type='virtio'/> <alias name='net0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <channel type='unix'> <source mode='bind' path='/var/lib/libvirt/qemu/channel/target/domain-88-gene01_16/org.qemu.guest_agent.0'/> <target type='virtio' name='org.qemu.guest_agent.0' state='disconnected'/> <alias name='channel0'/> <address type='virtio-serial' controller='0' bus='0' port='1'/> </channel> <input type='mouse' bus='ps2'> <alias name='input0'/> </input> <input type='keyboard' bus='ps2'> <alias name='input1'/> </input> <graphics type='spice' port='5906' autoport='yes' listen='127.0.0.1'> <listen type='address' address='127.0.0.1'/> </graphics> <audio id='1' type='spice'/> <video> <model type='cirrus' vram='16384' heads='1' primary='yes'/> <alias name='video0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <memballoon model='virtio'> <alias name='balloon0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </memballoon> <rng model='virtio'> <backend model='random'>/dev/urandom</backend> <alias name='rng0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </rng> </devices> <seclabel type='dynamic' model='apparmor' relabel='yes'> <label>libvirt-d1876d79-7fea-42b1-bd27-62a71809afaa</label> <imagelabel>libvirt-d1876d79-7fea-42b1-bd27-62a71809afaa</imagelabel> </seclabel> <seclabel type='dynamic' model='dac' relabel='yes'> <label>+64055:+108</label> <imagelabel>+64055:+108</imagelabel> </seclabel> </domain>
vnet48
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|