working on it ...

Filters

Explore Public Snippets

Sort by

Found 3 snippets matching: getops

    public by david-e  2868  15  5  0

    Bash getops example

    Simple example of argument parsing with getopts
    #!/bin/bash                                                                                                 
    usage()
    {   
        echo ""
        echo "$0 -f <csv|html> -i <IP address> -l"
        echo ""
        echo "Example:"
        echo " $0 -f html -i 172.16.88.83"
        echo " $0 -f csv  -i 172.16.88.83 -l"
    }
    
    #default value                                                                                              
    FORMAT="csv"
    IPADDR=""
    LOAD=false
    while getopts ":f:i:l" optname
    do  
        case "$optname" in
            "f")
                if [[ ${OPTARG} == "csv" || ${OPTARG} == "html" ]] ; then
                    FORMAT="${OPTARG}"
                else
                    echo "Output format not supported"
                    usage
                    exit 1
                fi
                ;;
            "i")
                IPADDR="${OPTARG}"
                ;;
            "l")
                LOAD=true
                ;;
            "?")
                usage
                exit 1
                ;;
            ":")
                echo "No argument value for option $OPTARG"
                ;;
            *)
                echo "Unknown error while processing options"
                ;;
        esac
    done
    
    # Mandatory arguments                                                                                       
    if [[ -z ${FORMAT} ]] || [[ -z ${IPADDR} ]]
    then
        usage
        exit 1
    fi
    
    echo "format: $FORMAT"
    echo "ipaddr: $IPADDR"
    if [[ ${LOAD} == "true" ]] ; then
        echo "load: true"
    else
        echo "load: false"
    fi
    

    external by brpaz  80  0  1  0

    Example how to use getops bash function to parse cmd line options. #bash

    Example how to use getops bash function to parse cmd line options. #bash: getops.sh
    #!/bin/sh
    
    # A POSIX variable
    OPTIND=1         # Reset in case getopts has been used previously in the shell.
    
    # Initialize our own variables:
    output_file=""
    verbose=0
    
    while getopts "h?vf:" opt; do
        case "$opt" in
        h|\?)
            show_help
            exit 0
            ;;
        v)  verbose=1
            ;;
        f)  output_file=$OPTARG
            ;;
        esac
    done
    
    shift $((OPTIND-1))
    
    [ "$1" = "--" ] && shift
    
    echo "verbose=$verbose, output_file='$output_file', Leftovers: $@"
    
    # End of file
    
    

    external by GiuseppeLota  4  0  1  0

    Bash getops template

    Bash getops template: getops_tmpl.sh
    usage() { echo "Usage: $0 [-s <45|90>] [-p <string>]" 1>&2; exit 1; }
    
    while getopts ":s:p:" o "$@"; 
    do
        case $o in
            s) s=${OPTARG}
                ((s == 45 || s == 90)) || usage;; 
    
            p)p=${OPTARG};;
    
            *)  usage ;;
        esac
    done
    shift $((OPTIND-1))
    
    if [ -z "${s}" ] || [ -z "${p}" ]; then
        usage
    fi
    
    echo "s = ${s}"
    echo "p = ${p}"
    
    # test command: -s: check if string is empty
    #               -f: check if file exist
    #               -s: check not empty  
    #               -ne: not equal (ex: INTEGER1 -ne INTEGER2)
    #               -z: the length of STRING is zero
    
    # 1>&2
    # 1 = standard output
    # 2 = standard error
    
    # "$@" - optional positional parameters except $0    
    # "$#" - the number of arguments, not counting $0
    # "$2" - second positional parameter
    
    

    List Complete ( 3 snippets total )

    • Public Snippets
    • Channels Snippets