Skip to content

Instantly share code, notes, and snippets.

@jlevon
Created May 3, 2018 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlevon/b3c306a408ffc0430ccdf73a5503c1f7 to your computer and use it in GitHub Desktop.
Save jlevon/b3c306a408ffc0430ccdf73a5503c1f7 to your computer and use it in GitHub Desktop.
vmadm alias completion
]] && [[ ${COMP_WORDS[COMP_CWORD-2]} == "vmadm" ]]; then
vms_uuids=$(zoneadm list -cp | grep -v ':global:' | cut -d':' -f5 | \
sort | uniq)
vms_aliases=$(vmadm list -Ho alias | sort -u)
gen=$(compgen -W "${vms_aliases}" -- ${cur})
if [ "$(echo $gen | wc -w)" -eq 1 ]; then
COMPREPLY=$(vmadm list -Ho uuid alias=$gen)
else
COMPREPLY=( $(compgen -W "${vms_uuids} ${vms_aliases}" -- ${cur}) )
fi
@mgerdts
Copy link

mgerdts commented May 3, 2018

You can get rid of wc by using an array.

[root@buglets ~]# typeset -a vms_aliases
[root@buglets ~]# vms_aliases=( $(vmadm list -Ho alias | sort -u) )
[root@buglets ~]# echo ${#vms_aliases[@]}
5
[root@buglets ~]# echo ${vms_aliases[@]}
docker-redis joyent log test test1710

This also has issues with multiple zones having the same alias and aliases with spaces (assuming that's a thing).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment