LoginSignup
34
25

More than 5 years have passed since last update.

shell scriptでのgetoptsの使い方メモ

Last updated at Posted at 2012-12-03
#!/bin/sh

while getopts l:a: OPT
do
    case $OPT in
        "l" ) echo "$OPTARG";;
        "a" ) echo "$OPTARG";;
    esac
done

getopts l:a: の部分がオプションの指定。
:(コロン)をつけることで、引数があることを明示。
引数があることを明示した場合、$OPTARGでその引数を取得することができる。

$ ./test.sh -l hoge -a fuga

$ hoge
$ fuga
と出力される。

参考: http://tech.lampetty.net/tech/index.php/archives/373

34
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
25