#navi(../)
* getoptsを利用して引数を取得する(bashビルドイン) [#meb5ae13]
今回利用するコマンドはgetopt''s''です。コマンドのgetopt%%s%%ではありません。~
getoptsは、bashのビルドインコマンドです。

#contents
#htmlinsertpcsp(linux_ads_top.html,linux-sp.html)

* 関連資料 [#v7018f31]
-[[シェルスクリプトに渡された引数の数を取得する方法>逆引きシェルスクリプト/シェルスクリプトに渡された引数の数を取得する方法]]
//-[[getoptsを利用して引数を取得する(bashビルドイン)>逆引きシェルスクリプト/getoptsを利用して引数を取得する(bashビルドイン)]]
-[[引数を配列に展開する方法>逆引きシェルスクリプト/引数を配列に展開する方法]]


* bashビルドインのgetoptsについて [#c0020eb0]
以下にgetoptsの使いかたを説明します。
** getopts引数:先頭の:の意味 [#f17ea5b7]
 getopts ":abc"
先頭に:が存在する場合は、abc以外のオプションが来た場合のエラー処理を自ら記述することを意味します。
-先頭に:がある場合
#ref(getopts-1.sh)
 #!/bin/bash
 # filename: getopts-1.sh
 while getopts ":abc" opts
 do
   case $opts in
     a)
       echo a
       ;;
     b)
       echo b
       ;;
     c)
       echo c
       ;;
   esac
 done
実行結果
 $ ./getopts-1.sh -abc
 a
 b
 c
 $ ./getopts-1.sh -a -b -c
 a
 b
 c
先頭に:があるためエラー出力されません。
 $ ./getopts-1.sh -d
 $

-先頭に:が無い場合
#ref(getopts-2.sh)
 #!/bin/bash
 # filename: getopts-2.sh
 while getopts "abc" opts
 do
   case $opts in
     a)
       echo a
       ;;
     b)
       echo b
       ;;
     c)
       echo c
       ;;
   esac
 done
実行結果
 $ ./getopts-2.sh -abcd
 a
 b
 c
 ./getopts-2.sh: illegal option -- d

** getopts引数:後ろの:の意味 [#g2eb0a9e]
後ろに:がある場合は、オプションの後ろに引数があるを指定します。
 getopts "abf:"
#ref(getopts-3.sh)
 #!/bin/bash
 # filename: getopts-3.sh
 while getopts "abf:" opts
 do
   case $opts in
     a)
       echo a
       ;;
     b)
       echo b
       ;;
     f)
       TARGET_FILE=$OPTARG 
       ;;
   esac
 done
 
 if [ "$TARGET_FILE" != "" ]; then
   echo TARGET = $TARGET_FILE
 fi
実行結果
 $ ./getopts-3.sh -a -f foo.txt
 a
 TARGET = foo.txt
 $ ./getopts-3.sh -fbar.txt
 TARGET = bar.txt
 $ ./getopts-3.sh -f
 ./getopts-3.sh: option requires an argument -- f

** 自分でエラー処理をする場合 [#l552eddf]
先頭に:を付けることによりオプションのエラー処理を自ら記述しなけらばなりません。~
以下に簡単なシェルスクリプト例と実行例を記述します。
#ref(getopts-4.sh)
 #!/bin/bash
 # filename: getopts-4.sh
 while getopts ":af:" opts
 do
   case $opts in
     a)
       echo a
       ;;
     f)
       TARGET_FILE=$OPTARG
       ;;
     :)
       echo "Option -$OPTARG requires an argument !!!" >&2
       ;;
     ¥?)
       echo "What is -$OPTARG option ???" >&2 
       ;;
   esac
 done
 
 if [ "$TARGET_FILE" != "" ]; then
   echo TARGET = $TARGET_FILE
 fi
実行結果
-不正なオプションを渡した場合の実行結果
 $ ./getopts-4.sh -z
 What is -z option???
\?)に記述された処理が実行されています。
-オプションに引数が必要なのに引数を渡さないで実行した結果
 $ ./getopts-4.sh -f
 Option -f requires an argument !!!
:)に記述された処理が実行されています。

* getoptsの変数 [#zac5556e]
getoptsには、以下の変数があります。
-OPTIND
-OPTARG
-OPTERR
** OPTARGの説明 [#d98e0ccc]
OPTARGについては上記のサンプルシェルスクリプトで使用しています。~
処理対象のオプションが格納されています。
** OPTERRの説明 [#j3d2734a]
OPTERRの初期値は1になります。~
この値を0にするとgetoptsからのエラー出力を抑止できます。~
以下のサンプルシェルスクリプトで説明します。
#ref(getopts-5.sh)
 #!/bin/bash
 # filename: getopts-5.sh
 echo Initial value: OPTERR=$OPTERR
 while getopts "af" opts
 do
   case $opts in
     a)
       echo a
       ;;
     f)
       OPTERR=0
       ;;
   esac
 done
- 不正なオプションを指定して実行
 $ ./getopts-5.sh -z
 Initial value: OPTERR=1
 ./getopts-5.sh: illegal option -- z
getoptsの引数の先頭に:がないのでgetoptsによりエラー処理が実行されているのがわかります。
 $ ./getopts-5.sh -f -z
 Initial value: OPTERR=1
上記は最初に-fオプションを指定しOPTERRの値を0にしています。~
したがって、-zがillegal optionなのですが、エラー表示が抑止されています。

** OPTINDの説明 [#yeea9471]
OPTINDの値を表示する簡単なシェルスクリプト
#ref(getopts-6.sh)
 #!/bin/bash
 # filename: getopts-6.sh
 while getopts "abc" opts
 do
   case $opts in
     a)
       echo a
       ;;
     b)
       echo b
       ;;
     c)
       echo c
       ;;
   esac
   echo OPTIND value is $OPTIND
 done
- 1つの引数として実行
 $ ./getopts-6.sh -abc
 a
 OPTIND value is 1
 b
 OPTIND value is 1
 c
 OPTIND value is 2
- オプションを個々に指定し実行
 $ ./getopts-6.sh -a -b -c
 a
 OPTIND value is 2
 b
 OPTIND value is 3
 c
 OPTIND value is 4
上記の結果よりOPTINDは引数の位置を管理しています。~
1つの引数として実行した場合は、1つの引数にabc全て含まれているのでOPTINDは1のまま処理され、処理終了後次の位置を示す2となっています。~
個々に指定した-a -b -cは1つずつ処理され次の位置を示す4となります。

OPTINDを使う場面は以下のような場合です。~

- getoptsで処理する以外の引数が存在する場合
以下にサンプルシェルスクリプトです。
#ref(getopts-7.sh)
 #!/bin/bash
 # filename: getopts-7.sh
 while getopts "abc" opts
 do
   case $opts in
     a)
       ;;
     b)
       ;;
     c)
       ;;
   esac
 done
 
 shift `expr $OPTIND - 1`
 echo $*
実行結果
 $ ./getopts-7.sh -a -b -c sakura
 sakura

shiftコマンドによりgetoptsで処理されたオプションが消されsakuraだけが残ります。~
したがって、getopts以外で使用する引数がある場合などは、OPTINDとshiftを使うことにより引数を残すことができます。~
以下のように記述することもできます。
 shift $((OPTIND-1))  # shift `expr $OPTIND - 1`

#htmlinsertpcsp(linux_ads_btm.html,linux-sp.html)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS