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 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 |
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.
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
To output the VBox status in terminal / ZSH (in the bottom right of the shell), add the following function to your
theme-name.zsh-theme
:(see: http://stackoverflow.com/questions/24274456/zsh-sticky-prompt)
Then just call the function in
build_prompt()
like so:I find this particularly handy in avoiding the case where I have a VM or two left running in the background unused and draining my battery!
Example:
No VMs running: https://www.evernote.com/shard/s153/sh/6bc2d85e-d9b7-4a69-8cf5-b831b0bd90ec/680ec902d7d540f94ace5aff5d80f6f6/res/5b3085c0-f003-4d26-ac9c-2fbe4d4ecccf/skitch.png?resizeSmall&width=832
1 VM running: https://www.evernote.com/shard/s153/sh/cf208c18-9d87-47d0-afb2-4b19c28662b8/8758ef7286dfc035381ab1e2f5d3c28f/res/8a47b88d-cb78-4519-9857-040a7c93e91c/skitch.png?resizeSmall&width=832