使用getopts处理选项和参数

5年以前  |  阅读数:498 次  |  编程语言:Shell 
#!/bin/bash
# processing options and parameters with getopts

while getopts :ab:cd opt
do
    case "$opt" in
    a) echo "Found the -a option";;
    b) echo "Found the -b option,with value $OPTARG";;
    c) echo "Found the -c option";;
    d) echo "Found the -d option";;
    *) echo "Unknown option: $opt";;
    esac
done
shift $[ $OPTIND - 1 ]
count=1
for param in "$@"
do
    echo "Parameter $count: $param"
    count=$[ $count + 1 ]
done

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8