Shell使用getopts读取参数

使用getopts可以处理

<command> [-i infile] outfile。不过还不知道怎么处理像<command> outfile [-i infile]。因为getopts遇到outfile时就会放弃分析。

usage='Usage: <command> [-i infile] outfile'
infile='/tmp/infile'
while getopts ":i:" opt; do
    case $opt in
        i)
            infile=$OPTARG
            ;;
        ?)
            echo $usage >&2
            exit 1;
            ;;
        :)
            echo "OPtion -$OPTARG requires an argument." >&2
            exit 1;
            ;;
    esac
done
shift `expr $OPTIND - 1`
outfile=$1
 

你可能感兴趣的:(shell)