【Linux】Shell 参数解析

#!/usr/bin/env bash

# example see  https://www.cnblogs.com/yxzfscg/p/5338775.html
# spring boot shell https://blog.csdn.net/qq_34208844/article/details/84874393

usage(){
    echo "请输入命令参数选项"
}

# 单个字母前带冒号表示参数的值不是必须的 前面有冒号表示参数必须有选项的值
while getopts 'd::ft:' OPT; do
    case ${OPT} in
        d)
            DEL_DAYS="$OPTARG";;
        f)
            DIR_FROM="$OPTARG";;
        t)
            DIR_TO="$OPTARG";;
        ?)
            echo "Usage: `basename $0` [options] filename"
     esac
done

# getopts在处理参数的时候,处理一个开关型选项(不带值选项),OPTIND加1,
# 处理一个带值的选项参数,OPTIND则会加2
if [[ $OPTIND -eq 1 ]]; then
    usage
fi