Add tags

Add tags to the following comment

pure bashで内蔵コマンドgetoptsを使いました。

起動例の-sはコマンドじゃなくて引数扱い、と解釈したために、その部分がBKっぽくなっています。

あと、-oは必須ということで、そこの出力は手抜きしています。

 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
OPTERR=0

args=()
nargs=0

opt_o=OFF
opt_q=OFF
opt_d=''

while [ $# != 0 ];do
    oprind_orig=$OPTIND
    while getopts 'oqd:*' opt;do
        case "$opt" in
        o)  opt_o=ON
            ;;
        q)  opt_q=ON
            ;;
        d)  opt_d=$OPTARG
            [[ $opt_d == [012] ]] || exit 1
            ;;
        \?) [[ ! ${!OPTIND} == -* ]] && ((OPTIND--))
            break
            ;;
        esac
    done
    shift $((OPTIND - 1))
    OPTIND=1
    [ $# != 0 ] && args[$((nargs++))]=$1
    shift
done

[ "$opt_o" = OFF ] && exit 1
((nargs)) || exit 1

echo "[オプション情報]
o(output): ON
q(quote):  $opt_q
d(debug):  $opt_d

[パラメータ情報]
指定数: $nargs"

for ((i = 0; i < nargs; i++));do
    echo "$((i + 1)): ${args[$i]}"
done

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...