VBoxManage : Get error code in bash script

Discussions related to using VirtualBox on Linux hosts.
Post Reply
jcdole
Posts: 95
Joined: 3. May 2013, 18:25
Primary OS: openSUSE
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: WIN 7, WIN 10
Location: South west of france

VBoxManage : Get error code in bash script

Post 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.
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows, Linux

Re: VBoxManage : Get error code in bash script

Post 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.
jcdole
Posts: 95
Joined: 3. May 2013, 18:25
Primary OS: openSUSE
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: WIN 7, WIN 10
Location: South west of france

Re: VBoxManage : Get error code in bash script

Post 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.
Post Reply