Last active
January 26, 2019 16:27
-
-
Save grega/61355d041ccfaef66419 to your computer and use it in GitHub Desktop.
Output the total number of currently running VirtualBox VMs, along with their names. This has been written with Vagrant in mind, where VMs are named `hostname_default_xxxxxxxxx` where only the value of `hostname` is relevant/recognisable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
machines=() | |
for machine in `VBoxManage list runningvms|cut -d" " -f 1`; do | |
machines+=("$machine") | |
done | |
if [ ${#machines[@]} -eq 1 ]; then | |
machinename=$(echo ${machines[@]} | cut -d'_' -f 1) | |
echo "1 VM running: $machinename" | |
elif [ ${#machines[@]} -gt 1 ]; then | |
echo "${#machines[@]} VMs running:" | |
for machine in ${machines[@]}; do | |
machinename=$(echo ${machine} | cut -d'_' -f 1) | |
echo "$machinename\"" | |
done | |
else | |
echo "No VMs running" | |
fi |
Thanks for the tput
related help @jcreasey!
I'll soon be looking at implementing this alongside Vagrant 1.6's 'global status' feature (rather than relying on VBox): http://www.vagrantup.com/blog/feature-preview-vagrant-1-6-global-status.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great job. I tried to get it to erase the prompt so that it wouldn't track up the terminal after you reach the bottom. On my mac it didn't support the erase commands though unfortunately.