Bash script - how to read file line by line while using getopts Bash script - how to read file line by line while using getopts shell shell

Bash script - how to read file line by line while using getopts


getopts only parses options (arguments starting with -); the other arguments are left alone. The parameter OPTIND tells you the index of the first argument not yet looked at; typically you discard the options before this.

while getopts "ab" opt; do    case "$opt" in    a) a=1       echo "a is enabled"       ;;    b) b=1       echo "b is enabled"       ;;    esacdoneshift $((OPTIND - 1))echo "$# arguments remaining"for arg in "$@"; do    echo "$arg"done

The preceding, if called as bash tmp.bash -a -b c d e, produces

$ bash tmp.bash -a -b c d ea is enabledb is enabled3 arguments remaining:cde