1

I have to write a script for linux-box which takes input from command line sudo build_all.sh -vmware yes|no nic bridged|host_only -ipaddress xx.xx.xx.xx|dhcp -arch=ARM|x86, I was wondering if there is a way while entering input i can get suggestion. For Ex.

if i enter sudo build_all.sh and press Tab i get the suggestion like -vmware after entering -vmware i again tree Tab the i promted for the Yes No.

Is there any way?

TaZz
  • 652
  • 7
  • 20
Arun Gupta
  • 810
  • 2
  • 9
  • 26

2 Answers2

1

This is called bash completion. You can find many examples in /etc/bash_completion.d/ . The bash_completion function needs to be loaded to your shell before it can work. This is an example to get you started

# Bash completion must to be loaded when shell starts
# Recommended location is /etc/bash_completion.d/build_all.sh
# or added to ~/.bashrc
# Then load with . /etc/bash_completion.d/build_all.sh
# or with . ~/.bashrc
# New instances of bash will have already sourced it. 
# The file name build_all.sh may be too common, and result in unwanted tab
# completion for build_all.sh from other projects. 

_build_all.sh(){
    local cur
    COMPREPLY=()
    _get_comp_words_by_ref cur

    case  $COMP_CWORD in
        1) COMPREPLY=( $( compgen -W '-vmware' -- "$cur" ) ) ;;
        2) COMPREPLY=( $( compgen -W 'no yes' -- "${COMP_WORDS[COMP_CWORD]}" ) ) ;;

        3) COMPREPLY=( $( compgen -W '-nic' -- "$cur" ) ) ;;
        4) COMPREPLY=( $( compgen -W 'host_only bridged' -- "${COMP_WORDS[COMP_CWORD]}" ) ) ;;

        5) COMPREPLY=( $( compgen -W '-ipaddress' -- "$cur" ) ) ;;
        6) COMPREPLY=( $( compgen -W 'dhcp xx.xx.xx.xx' -- "${COMP_WORDS[COMP_CWORD]}" ) ) ;;

        7) COMPREPLY=( $( compgen -W '-arch' -- "$cur" ) ) ;;
        8) COMPREPLY=( $( compgen -W 'x86 ARM' -- "${COMP_WORDS[COMP_CWORD]}" ) ) ;;
    esac
    return 0

} &&
complete -F _build_all.sh build_all.sh
Keith Reynolds
  • 833
  • 6
  • 12
  • Can't we have a way putting auto completion in my bash script? or i have to make changes to `/etc/bash_completion.d/` always. Actually this script is going to run in different Linux who downloads this Package? Any suggestion – Arun Gupta Apr 07 '14 at 04:37
  • "Can't we have a way putting auto completion in my bash script?" No! You can pace your bash completions scripts in /etc/bash_completion.d/ or load it from your ~/.bashrc Please mark if you found this helpful. – Keith Reynolds Apr 08 '14 at 22:20
0

There's a program called whiptail that can do "GUI" dialogs in text mode.

http://www.techrepublic.com/blog/linux-and-open-source/how-to-use-whiptail-to-write-interactive-shell-scripts/

BraveNewCurrency
  • 12,654
  • 2
  • 42
  • 50