Skip to content

Instantly share code, notes, and snippets.

@ketanarlulkar
Last active June 22, 2017 07:09
Show Gist options
  • Save ketanarlulkar/56576d01dba96349f095fd1cf09abf55 to your computer and use it in GitHub Desktop.
Save ketanarlulkar/56576d01dba96349f095fd1cf09abf55 to your computer and use it in GitHub Desktop.
An introduction to bash completion

One of the nicest facilities of the modern shell is the built in "completion" support. These facilities allow you to complete commands and their arguments easily. Read on for a brief introduction to adding your own command completions. Most shells allow command completion, typically bound to the TAB key, which allow you to complete the names of commands stored upon your PATH, file names, or directory names. This is typically used like so:

  `ls /bo[TAB]`

When you press the TAB key the argument /bo is automatically replaced with the value /boot.

  1. Install hdparm tool
  2. Copy hdparm file to /etc/bash_completion.d/hdparm
  3. Source this /etc/bash_completion.d/hdparm file using following command.

. /etc/bash_completion.d/hdparm 4. Now try hdparm command and check if auto completion is working or not.

Learn from here

_hdparm()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="-a -A -b -B -c -C -d -D -E -f -F -g -h -H -i -I -J -k -K -L -m -M -n -N -p -P -q -Q -r -R -s -S -t -T -u -U -v -V -w -W -x -X -y -Y -z -Z --dco-freeze --dco-identify --dco-restore --direct --drq-hsm-error --fallocate --fibmap --fwdownload --fwdownload-mode3 --fwdownload-mode3-max --fwdownload-mode7 --idle-immediate --idle-unload --Istdin --Istdout --make-bad-sector --offset --prefer-ata12 --read-sector --security-help --trim-sector-ranges --trim-sector-ranges-stdin --verbose --write-sector --please-destroy-my-drive --yes-i-know-what-i-am-doing"
case "${prev}" in
-I|-i)
local running=$(for x in `ls -1 /dev/sd*`; do echo ${x//} ; done)
COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
return 0
;;
--please-destroy-my-drive)
COMPREPLY=( $(compgen -W "--yes-i-know-what-i-am-doing" -- ${cur}) )
return 0
;;
--fwdownload|--fwdownload-mode3|--fwdownload-mode3-max|--fwdownload-mode7|--fibmap)
local running=$(for x in `ls -1`; do echo ${x} ; done)
COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
return 0
;;
*)
;;
esac
if [[ ${cur} == -* ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
}
complete -F _hdparm hdparm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment