Take parts of the standard output value and put it into a array variable

193 views Asked by At

I'm currently working on a script (using BASH) which backups VM file to a remote server.

I want to try and make the script a bit more dynamic by being able to just looping though each VM from a "show VM command". my idea is to take the standard output of a command which show all the VM and break up and turn it to useful variables. possibly a multi-array.

the Output comes out like this is there anyway to break it all up? say by spaces and line breaks?

Vmid         Name                                  File                                  Guest OS           Version   Annotation
10     FREEPBX             [datastore2] FREEPBX/FREEPBX.vmx                       other26xLinux64Guest      vmx-08              
13     AdaptivNICE2Cloud   [datastore2] AdaptivNICE2Cloud/AdaptivNICE2Cloud.vmx   other26xLinux64Guest      vmx-08              
15     IVSTelManager       [datastore2] IVSTelManager/IVSTelManager.vmx           debian6Guest              vmx-08              
4      Neptune             [datastore1] Neptune/Neptune.vmx                       winNetEnterprise64Guest   vmx-08              
9      Kayako              [datastore2] Kayako/Kayako.vmx                         other26xLinux64Guest      vmx-08 
1

There are 1 answers

1
anishsane On

I guess you need this:

$ vim-cmd vmsvc/getallvms | sed -n 's|.*\[|/vmfs/volumes/|;s|\] *|/|;s|\.vmx .*|.vmx|p'
/vmfs/volumes/datastore2/FREEPBX/FREEPBX.vmx                                    
/vmfs/volumes/datastore2/AdaptivNICE2Cloud/AdaptivNICE2Cloud.vmx                
/vmfs/volumes/datastore2/IVSTelManager/IVSTelManager.vmx                        
/vmfs/volumes/datastore1/Neptune/Neptune.vmx                                    
/vmfs/volumes/datastore2/Kayako/Kayako.vmx                         
# Prints all VMX files paths

OR

$ vim-cmd vmsvc/getallvms | sed -n 's|.*\[|/vmfs/volumes/|;s|\] *|/|;s|/[^/]*\.vmx .*||p'
/vmfs/volumes/datastore2/FREEPBX
/vmfs/volumes/datastore2/AdaptivNICE2Cloud
/vmfs/volumes/datastore2/IVSTelManager
/vmfs/volumes/datastore1/Neptune
/vmfs/volumes/datastore2/Kayako
# Prints all directories having VMX files. These directories also contain the virtual HDDs, which you would want to backup.

(Ignore the $ in the prompt; it is still root prompt. SO would interpret it as comment if I use # in place if $..)