Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Thursday, 26 September 2013

BASH completion example

Since sometimes we forget (or can't be bothered) with doing a --help or doing a man <command> having bash completion is great.

in this example, I am using a script that I've made which is a simple script that has the following commands.

vm.sh --help
create [servername] If option=console, then display console immediately
destroy [servername] Delete a VM
list-vol List all disks for all VMs
list-vm List VMs and status
console [servername] Access console of server
vnc [servername] List VNC port for VM
reset [servername] Restart VM

With bash completion, after typing the command (vm.sh) and pressing tab it will automatically show the commands create destroy list-vol list-vm console vnc reset

Then after the command, pressing tab will output the list of all servers available.

# bash completion for vm.sh
# pks - 20130926
# Now with extra flash bastard bits

# Press tab to view list of available servers after the command


_vm.sh ()   #  By convention, the function name
{           #+ starts with an underscore.
  local cmd_="$1##*/}"
  # word that is being completed
  local word=${COMP_WORDS[COMP_CWORD]}
  # entire line being completed
  local line=${COMP_LINE}
  # exclusion pattern
  local xpat='|*mqueue*'
  _get_comp_words_by_ref -n : cur prev
  # previous command
  prev="${COMP_WORDS[COMP_CWORD-1]}"

  COMPREPLY=()   # Array variable storing the possible completions.
  case "$prev" in
    create|destroy|console|vnc|reset)
    servers=`vm.sh list-vm | awk '{print $2}' | grep web | xargs -n1`
    COMPREPLY=( $( compgen -W '$servers' -- "$cur" ) )
    return 0
    ;;

  esac
  case "$word" in
    *)
    COMPREPLY=( $( compgen -W 'create destroy list-vol list-vm console \
                               vnc reset' -- $cur ) );;
  esac

  return 0
}

_complete ()
{

  COMPREPLY=
}
complete -F _vm.sh vm.sh


# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh

To break it down,
$word contains the current command
$prev contains the previous command, so if it is selected, what the next command can be e.g. in this case
if prev=console then the next command can be <servername>

Tuesday, 16 August 2011

advanced BASH scripting

Simple things:
To assign a variable to an array:

array_config_timestamp[$i]="${array_config[$i]:0:16}"
Breaking this down, $i = index
0:16 is the substring position within the string

(( i++ ))
adds  1 to the variable i
max_updates=$(( $j-1 ))
j =j-1

Converting seconds to HH:MM:SS

hours=$(( ${array_error_time[$i]} / 3600 ))
seconds=$(( ${array_error_time[$i]} % 3600 ))
minutes=$(( $seconds / 60 ))
seconds=$(( $seconds % 60 ))

So basically (( )) interprets the expression within, % is the basic arithmetic function mod


 array_error_updatei[$error_index]=`/usr/bin/expr index "${array_index_name[$j]:10}" _`
This searches for the first occurence of _ within the array array_index_name with index j from position 10

printf "%s %d\x68%d\x6d%d\x73" ${array_error[$i]:9} $hours $minutes $seconds
prints formatted text ${array_error[$i]:9} and HH:MM:SS as 1h 2m 3s

Wednesday, 4 May 2011

DASH BASH

Well dash is being introduced and is more POSIX compliant than bash

Here's a link to show you the differences:

http://mywiki.wooledge.org/Bashism