#!/usr/bin/env bash #while getopts a:b name; do # echo name: "$name", OPTARG: "$OPTARG", OPTIND: $OPTIND, OPTERR: "$OPTERR" #done echo "$*" echo "$@" status=off filename= output= usage() { echo Usage: echo "$0 [-h] [-v] [-f <filename>] [-o <filename>]" exit 0 } while getopts :hvf:o: name; do case "$name" in h) echo name: "$name", OPTARG: "$OPTARG", OPTIND: $OPTIND, OPTERR: "$OPTERR" usage ;; v) echo name: "$name", OPTARG: "$OPTARG", OPTIND: $OPTIND, OPTERR: "$OPTERR" status=on ;; f) echo name: "$name", OPTARG: "$OPTARG", OPTIND: $OPTIND, OPTERR: "$OPTERR" filename="$OPTARG" if [ ! -f "$filename" ]; then echo filename: "$filename" does not exist exit 5 fi ;; o) echo name: "$name", OPTARG: "$OPTARG", OPTIND: $OPTIND, OPTERR: "$OPTERR" output="$OPTARG" if [ ! -f "$output" ]; then echo output: "$output" does not exist exit 5 fi ;; '?') # ? have to be quoted, because it's a wildcard, or \? echo name: "$name", OPTARG: "$OPTARG", OPTIND: $OPTIND, OPTERR: "$OPTERR" echo invalid option -"$OPTARG" exit 6 ;; :) echo name: "$name", OPTARG: "$OPTARG", OPTIND: $OPTIND, OPTERR: "$OPTERR" echo option -"$OPTARG" require a argument exit 7 ;; esac done echo status: $status, filename: "$filename", output: "$output",
标签:name,OPTIND,getopts,echo,OPTERR,filename,OPTARG From: https://www.cnblogs.com/dissipate/p/16874748.html