Bash autocomplete

0

I am creating an interactive menu in Bash where I want to be writing a text entry, at the same time read a file.txt and with that information show us options to autocomplete our input, something like the autocomplete in jquery. The question is: Is there any way to do this? the code is:

if [ -z "$1" ]; then

        echo "========"
        read -e -p "HOSTNAME: " HOST
        ./menu.bash $HOST
elif [ -n "$2" ]; then

        clear
        echo "ssh $(whoami)@$1"
else

        HOST=$1
        cat ../data/hosts | egrep ".+\s+\$HOST"
        echo "========"
        read -e -p "HOSTNAME: " -i "$HOST" HOST2
        ./menu.bash $HOST2
fi
    
asked by Jorge Alberto Tovar Meza 03.09.2018 в 18:03
source

1 answer

1

See this site bash-programmable-completion-tutorial

can be helpful, you can try this. create a autocomplete.sh file with the following

#/usr/bin/env bash

function main(){

COMANDO="$@"
DIR='/bin/'$COMANDO

if [ -f $DIR  ]; then
    echo "YA ESTA CREADO"
    $COMANDO
else

    echo "
#/usr/bin/env bash

if [ -z \"$1\" ]; then
  echo \"Ingrese el comando $COMANDO <tab><tab>\"
  exit 2
fi

exists=$(fc -l -1000 | grep ^$1 -- 2>/dev/null)

if [ -n \"$exists\" ]; then
  fc -s -- \"$1\"
else
  echo \"Command with number $1 was not found in recent history\"
  exit 2
fi
    ">$DIR

    chmod +x $DIR

    echo "este es el contenido de prueba del archivo plano">$DIR-test.txt

    echo "
#/usr/bin/env bash
complete -W \"$(cat "$DIR-test.txt")\" $COMANDO
        ">$DIR'-completion.bash'

        chmod +x+w $DIR'-completion.bash'
        source $DIR'-completion.bash'

        echo "HECHO" && $COMANDO

    fi
}

main "$@"

Then run it in the terminal

~$ sudo chmod +x autocompletar.sh && ./autocompletar.sh autoconpletar
~$ source /bin/autoconpletar'-completion.bash'
~$ autoconpletar <tab><tab>

ready will show the contents of the file.txt

    
answered by 12.09.2018 / 08:03
source