From 28f631e02bbf7e4cbd763fc74c4941d3f55a030f Mon Sep 17 00:00:00 2001 From: johannst Date: Sat, 19 Sep 2020 13:57:06 +0000 Subject: deploy: 4a5cd61b7c536ecf1bdb288cb6d584c190b1f6c7 --- tools/bash.html | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/bash.html b/tools/bash.html index 7d74cc9..f7278b6 100644 --- a/tools/bash.html +++ b/tools/bash.html @@ -233,6 +233,42 @@ command 2>&1 >file
  • duplicate fd 1 to fd 2, effectively redirecting stderr to stdout
  • redirect stdout to file
  • +

    Argument parsing with getopts

    +

    The getopts builtin uses following global variables:

    + +
    getopts <optstring> <param> [<args>]
    +
    + +

    Example

    +
    #!/bin/bash
    +function parse_args() {
    +	while getopts "f:c" PARAM; do
    +		case $PARAM in
    +			f) echo "GOT -f $OPTARG";;
    +			c) echo "GOT -c";;
    +			*) echo "ERR: print usage"; exit 1;;
    +		esac
    +	done
    +	# users responsibility to reset OPTIND
    +	OPTIND=0
    +}
    +
    +parse_args -f xxx -c
    +parse_args -f yyy
    +

    Completion

    The complete builtin is used to interact with the completion system.

    complete                    # print currently installed completion handler
    @@ -253,7 +289,7 @@ COMPREPLY       # array with possible completions
     

    The compgen builtin is used to generate possible matches by comparing word against words generated by option.

    -
    compgen [option] [word]
    +
    compgen <option> <word>
     
     # usefule options:
     # -W <list>    specify list of possible completions
    @@ -268,7 +304,7 @@ compgen -W "foo foobar bar" "f"
     # compare "hom" against file/dir names and generate matches
     compgen -d -f "hom"
     
    -

    Example

    +

    Example

    Skeleton to copy/paste for writing simple completions.

    Assume a program foo with the following interface:

    foo -c green|red|blue -s low|high -f <file> -h
    -- 
    cgit v1.2.3