許容可能なオプションとパラメータのshellスクリプトの作成
許容可能なオプションとパラメータのshellスクリプトの作成
単純なオプションの操作
スクリプト名が
実行:
出力結果:
パラメータとオプションの分離
オプションがパラメータ、例えば
実行:
出力結果:
値付きオプションの処理
実行:
出力結果:
複雑なオプションの処理
このうち
注意:getoptコマンドは、スペースと引用符付きのパラメータ値を処理するのが苦手です.二重引用符ではなく、スペースをパラメータ区切り記号として使用します.
実行:
結果出力:
もう1つの使いやすい機能は、スペースを追加することなく、オプションのアルファベットとパラメータ値を一緒に使用することです.
完全な例
一般的なLinuxコマンドオプション仕様
オプション
説明
-a
すべてのオブジェクトを表示
-c
カウントを生成
-d
ディレクトリの指定
-e
オブジェクトを拡張
-f
データを読み込むファイルの指定
-h
コマンドのヘルプ情報を表示
-i
テキストの大文字と小文字を無視
-l
出力を生成するロングフォーマットバージョン
-n
非対話モードの使用(バッチ)
-o
すべての出力を指定した出力ファイルにリダイレクト
-q
サイレントモードで動作
-r
ディレクトリとファイルを再帰的に処理
-s
サイレントモードで動作
-v
詳細出力の生成
-x
オブジェクトを除外
-y
すべての質問に答えるyes
単純なオプションの操作
スクリプト名が
test.sh
、オプションが-a -b -c
の形式であると仮定します.#!/bin/bash
# extracting command line options as parameters
#
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
*) echo "$1 is not an option" ;;
esac
shift
done
実行:
./test.sh -a -b -c -d
出力結果:
Found the -a option
Found the -b option
Found the -c option
-d is not an option
パラメータとオプションの分離
オプションがパラメータ、例えば
-a -b -c -- test1 test2 test3
の形式である場合.#!/bin/bash
# extracting options and parameters
echo
#
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option";;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
done
#
count=1
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
実行:
./test.sh -a -b -c -- test1 test2 test3
出力結果:
Found the -c option
Found the -a option
Found the -b option
Parameter #1: test1
Parameter #2: test2
Parameter #3: test3
値付きオプションの処理
#!/bin/bash
# extracting command line options and values
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift ;;
-c) echo "Found the -c option";;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
done
#
count=1
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
実行:
./test.sh -a -b test1 -d
出力結果:
Found the -a option
Found the -b option, with parameter value test1
-d is not an option
複雑なオプションの処理
getopt
コマンドは、一連の任意の形式のコマンドラインオプションおよびパラメータを受け入れ、自動的に適切なフォーマットに変換することができる.コマンドのフォーマットは次のとおりです.getopt optstring parameters
このうち
optstring
は、コマンドラインに有効なオプションアルファベットを定義し、パラメータ値が必要なオプションアルファベットも定義します.まず、スクリプトで使用する各コマンドラインオプションのアルファベットをoptstring
にリストします.次に、パラメータ値が必要なオプションアルファベットごとにコロンを付けます.getopt
コマンドは、定義したoptstring
に基づいて、提供されたparameters
を解析します.getopt ab:cd -a -b test1 -cd test2 test3
#
# -a -b test1 -c -d -- test2 test3
#!/bin/bash
# Extract command line options & values with getopt
# getopt ,-q
# set (--), set
set -- $(getopt -q ab:cd "$@")
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift ;;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
done
#
count=1
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
./test.sh -ac
Found the -a option
Found the -c option
注意:getoptコマンドは、スペースと引用符付きのパラメータ値を処理するのが苦手です.二重引用符ではなく、スペースをパラメータ区切り記号として使用します.
./test.sh -a -b test1 -cd "test2 test3" test4
Found the -a option
Found the -b option, with parameter value 'test1'
Found the -c option
Parameter #1: 'test2
Parameter #2: test3'
Parameter #3: 'test4'
getopts
コマンドはgetopt
とは異なり、前者はコマンドライン上のオプションとパラメータを処理した後に1つの出力しか生成しないが、getopts
コマンドは既存のshellパラメータ変数と暗黙の了解を得ることができる.getopts optstring variable
optstring
の値は、getopt
コマンドの値に似ています.有効なオプション文字はoptstring
に表示され、パラメータ値にスペースを含めることができます.オプションのアルファベットにパラメータ値が必要な場合は、コロンを付けます.エラーメッセージを削除するには、optstring
の前にコロンを付けることができます.getopts
コマンドは、現在のパラメータをコマンドラインで定義されたvariable
に保存します.#!/bin/bash
# simple demonstration of the getopts command
#
echo
while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option, with value $OPTARG";;
c) echo "Found the -c option" ;;
*) echo "Unknown option: $opt";;
esac
done
実行:
./test.sh -ab test1 -c
結果出力:
Found the -a option
Found the -b option, with value test1
Found the -c option
もう1つの使いやすい機能は、スペースを追加することなく、オプションのアルファベットとパラメータ値を一緒に使用することです.
./test.sh -abtest1
Found the -a option
Found the -b option, with value test1
完全な例
#!/bin/bash
# Processing options & parameters with getopts
#
echo
while getopts :ab:cd opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option, with value $OPTARG" ;;
c) echo "Found the -c option" ;;
d) echo "Found the -d option" ;;
*) echo "Unknown option: $opt" ;;
esac
done
#
shift $[ $OPTIND - 1 ]
#
echo
count=1
for param in "$@"
do
echo "Parameter $count: $param"
count=$[ $count + 1 ]
done
OPTIND
環境変数は、パラメータリストのgetopts
が処理しているパラメータの位置を保存します.これにより、オプションを処理した後、他のコマンドラインパラメータの処理を続行できます.一般的なLinuxコマンドオプション仕様
オプション
説明
-a
すべてのオブジェクトを表示
-c
カウントを生成
-d
ディレクトリの指定
-e
オブジェクトを拡張
-f
データを読み込むファイルの指定
-h
コマンドのヘルプ情報を表示
-i
テキストの大文字と小文字を無視
-l
出力を生成するロングフォーマットバージョン
-n
非対話モードの使用(バッチ)
-o
すべての出力を指定した出力ファイルにリダイレクト
-q
サイレントモードで動作
-r
ディレクトリとファイルを再帰的に処理
-s
サイレントモードで動作
-v
詳細出力の生成
-x
オブジェクトを除外
-y
すべての質問に答えるyes