简单的getopts代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash
 
usage() { echo "Usage: $0 [-s <45|90>] [-p <string>]" 1&gt;&2; exit 1; }
 
while getopts ":s:p:" o; do
    case "${o}" in
        s)
            s=${OPTARG}
            ((s == 45 || s == 90)) || usage
            ;;
        p)
            p=${OPTARG}
            ;;
        *)
            usage
        ;;
    esac
done
 
shift $((OPTIND-1))
 
if [ -z "${s}" ] || [ -z "${p}" ]; then
    usage
fi
 
echo "s = ${s}"
echo "p = ${p}"