读书人

Shell施用getopts读取参数

发布时间: 2012-10-25 10:58:57 作者: rapoo

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;            ;;    esacdoneshift `expr $OPTIND - 1`outfile=$1
?

读书人网 >编程

热点推荐