11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

_killall ()<br />

{<br />

local cur prev<br />

COMPREPLY=()<br />

cur=${COMP_WORDS[COMP_CWORD]}<br />

}<br />

# get a list of processes (the first sed evaluation<br />

# takes care of swapped out processes, the second<br />

# takes care of getting the basename of the process)<br />

COMPREPLY=( $( /usr/bin/ps -u $USER -o comm | \<br />

sed -e ’1,1d’ -e ’s#[]\[]##g’ -e ’s#^.*/##’| \<br />

awk ’{if ($0 ~ /^’$cur’/) print $0}’ ))<br />

return 0<br />

complete -F _killall killall killps<br />

Appen<strong>di</strong>ce K. Un esempio <strong>di</strong> file .bashrc<br />

# A meta-command completion function for commands like sudo(8), which need to<br />

# first complete on a command, then complete accor<strong>di</strong>ng to that command’s own<br />

# completion definition - currently not quite foolproof (e.g. mount and umount<br />

# don’t work properly), but still quite useful - By Ian McDonald, mo<strong>di</strong>fied by me.<br />

_my_command()<br />

{<br />

local cur func cline cspec<br />

COMPREPLY=()<br />

cur=${COMP_WORDS[COMP_CWORD]}<br />

if [ $COMP_CWORD = 1 ]; then<br />

COMPREPLY=( $( compgen -c $cur ) )<br />

elif complete -p ${COMP_WORDS[1]} &>/dev/null; then<br />

cspec=$( complete -p ${COMP_WORDS[1]} )<br />

if [ "${cspec%%-F *}" != "${cspec}" ]; then<br />

# complete -F <br />

#<br />

# COMP_CWORD and COMP_WORDS() are not read-only,<br />

# so we can set them before han<strong>di</strong>ng off to regular<br />

# completion routine<br />

# set current token number to 1 less than now<br />

COMP_CWORD=$(( $COMP_CWORD - 1 ))<br />

# get function name<br />

func=${cspec#*-F }<br />

func=${func%% *}<br />

# get current command line minus initial command<br />

cline="${COMP_LINE#$1 }"<br />

# split current command line tokens into array<br />

COMP_WORDS=( $cline )<br />

$func $cline<br />

804

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!