使用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

本文详细探讨了如何使用getopts命令处理输入参数,特别关注了处理不同顺序的参数输入,如使用文件名作为命令参数或作为选项参数的情况,并提供了实例代码和解析流程。
474

被折叠的 条评论
为什么被折叠?



