Page 1 of 1

VBoxManage : Get error code in bash script

Posted: 14. Jul 2020, 23:27
by jcdole
Hello.

In a bash script this command return a normal error (because $L_VM_NAME2 is empty for the test) :

Code: Select all

    VBoxManage showvminfo "$L_VM_NAME2" | grep MAC | awk '{print $4}' | cut -d',' -f1

    VBoxManage: error: Could not find a registered machine named ''
    VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBoxWrap, interface IVirtualBox, callee nsISupports
    VBoxManage: error: Context: "FindMachine(Bstr(VMNameOrUuid).raw(), machine.asOutParam())" at line 2699 of file VBoxManageInfo.cpp

I can catch the batch error code with :

Code: Select all

 VBoxManage showvminfo "$L_VM_NAME2" | grep MAC | awk '{print $4}' | cut -d',' -f1
    L_RET_CODE1="${PIPESTATUS[0]}"
    echo "L_RET_CODE1 : $L_RET_CODE1"

    VBoxManage: error: Could not find a registered machine named ''
    VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBoxWrap, interface IVirtualBox, callee nsISupports
    VBoxManage: error: Context: "FindMachine(Bstr(VMNameOrUuid).raw(), machine.asOutParam())" at line 2699 of file VBoxManageInfo.cpp
    L_RET_CODE1 : 1
In normal situation without error, I need the value return by the command. So I store the result in a variable.

Code: Select all

    L_VM_MAC_ADDR_VALUE1=$(VBoxManage showvminfo "$L_VM_NAME2" | grep MAC | awk '{print $4}' | cut -d',' -f1)
    L_RET_CODE2="${PIPESTATUS[0]}"
    echo "L_RET_CODE2 : $L_RET_CODE2"
    echo "L_VM_MAC_ADDR_VALUE1 : $L_VM_MAC_ADDR_VALUE1"
and got :

Code: Select all

    VBoxManage: error: Could not find a registered machine named ''
    VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBoxWrap, interface IVirtualBox, callee nsISupports
    VBoxManage: error: Context: "FindMachine(Bstr(VMNameOrUuid).raw(), machine.asOutParam())" at line 2699 of file VBoxManageInfo.cpp
    L_RET_CODE2 : 0
    L_VM_MAC_ADDR_VALUE1 :
I need help to know if the command end with or without an error by testing 'L_RET_CODE2'.

Any comment is welcome.

Re: VBoxManage : Get error code in bash script

Posted: 16. Jul 2020, 17:48
by scottgus1
I suspect this is typical Linux command-line usage, not a Virtualbox problem. Vboxmanage is apparently running correctly, but the way you're using the command in the script isn't responding well.

I see that you can get the error code when running the command directly, but when running the command in a variable assignation step the error code does not get assigned.

Could it be possible (being a Windows guy I don't know) that since the command runs inside the assign part of a previous command, the error code might only be available inside the scope of the previous command but not outside that scope? Just guessing, really.

I would suggest leaving the vboxmanage command running directly in the batch file instead of nesting it inside another command, then adding to the grep awk stuff to get the MAC address as another variable within the vboxmanage command.

Re: VBoxManage : Get error code in bash script

Posted: 2. Aug 2020, 17:55
by jcdole
as far as I know, this is the role of pipestatus. ( the status of the pipe elements )
So in my case "${PIPESTATUS[0]}" should return the status of the first command.

Of course your suggestion works ( I already use it ).

This is much a bash + virtualbox question.

Any way thank you for helping.