linux shellコマンドラインのオプションとパラメータの使い方の詳細

5581 ワード

問題の説明:tail-n 10 access.logのようなコマンドラインオプションは、linux shellでどのように処理されますか?bashでは、コマンドラインパラメータを以下の3つの方法で処理することができます。それぞれの方法には、自分のアプリケーションシーンがあります。1,直接処理して、順次$1、$2、...、$nを解析して、それぞれ手作業で処理します。2、getoptsは処理に来て、単一の文字のオプションの場合(例えば、-n 10-f file.txtなど);3、getoptは、単一の文字オプションを処理してもいいし、長オプションlong-option(例えば、prefix=/homeなど)を処理してもいいです。まとめ:小さい脚本は手作業で処理すればいいです。getoptsは圧倒的に多くの状況を処理できます。getoptは複雑で、機能ももっと強いです。1,直接手で位置パラメータを処理するにはいくつかの変数が必要です。
 
   *    $0 : , c/c++ argv[0]
    *    $1 : .
    *    $2, $3, $4 ... : 2、3、4 , 。
    *    $#  ,
    *    $@ : ,
    *    $* : $@ , "$*" "$@"( ) ,"$*" , "$@" 。
の手で処理すると多くの簡単なニーズを満たすことができます。shiftに合わせて使うと強力な機能も構築できますが、複雑なオプションを扱う場合は次の2つの方法がおすすめです。例えば、(getags.sh):

#!/bin/bash
if [ $# -lt 1 ]; then
    echo "error.. need args"
    exit 1
fi
echo "commond is $0"
echo "args are:"
for arg in "$@"
do
    echo $arg
done
実行コマンド:

./getargs.sh 11 22 cc
commond is ./getargs.sh
args are:
11
22
cc
2、getopts(shell内蔵命令)処理コマンドラインパラメータは似たような複雑なものであり、そのために、cはgetopt/getopt_を提供している。longなどの関数は、c++のbootsがoptionsライブラリを提供し、shellでこのことを処理しているのはgetoptsとgetoptです。getopts/getoptの違いは、getoptは外部binaryファイルで、getoptsはshell builtinです。

[root@jbxue ~]$ type getopt
getopt is /usr/bin/getopt
[root@jbxue ~]$ type getopts
getopts is a shell builtin
getoptsは直接に長いオプションを処理することができません。  検索getoptsには2つのパラメータがあります。最初のパラメータは文字列で、文字と「:」を含みます。各文字は有効なオプションです。もし文字の後ろに「:」があるなら、この文字は自分のパラメータがあることを示します。これらのパラメータをコマンドから取得し、「-」を削除し、自身のパラメータを持つ場合は、2番目のパラメータに値を割り当てます。getoptsを提供するshellには、optargという変化が内蔵されています。getoptsはこの変数を修正しました。ここで変数$optargは対応するオプションのパラメータを格納しますが、optingは常に元の$*の次の処理する要素の位置を記憶します。while getopts::a:bc"opt  #最初のコロンは無視エラーを表します。文字の後のコロンは、このオプションは自分のパラメータの例が必要であることを示しています。(getopts.sh):

echo $*
while getopts ":a:bc" opt
do
        case $opt in
                a ) echo $optarg
                    echo $optind;;
                b ) echo "b $optind";;
                c ) echo "c $optind";;
                ? ) echo "error"
                    exit 1;;
        esac
done
echo $optind
shift $(($optind - 1))
# shift $(($optind - 1)) ,$* , shell 。
echo $0
echo $*
実行コマンド:

./getopts.sh -a 11 -b -c
-a 11 -b -c
11
3
b 4
c 5
5
./getopts.sh
、getopt(外部ツール)具体的な使い方は、man getopt(*)を表します。(getopt.sh):

#!/bin/bash
# a small example program for using the new getopt(1) program.
# this program will only work with bash(1)
# an similar program using the tcsh(1) script. language can be found
# as parse.tcsh
# example input and output (from the bash prompt):
# ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long "
# option a
# option c, no argument
# option c, argument `more'
# option b, argument ` very long '
# remaining arguments:
# --> `par1'
# --> `another arg'
# --> `wow!*\?'
# note that we use `"$@"' to let each command-line parameter expand to a
# separate word. the quotes around `$@' are essential!
# we need temp as the `eval set --' would nuke the return value of getopt.
#-o , ,
# -carg -c arg
#--long
#"$@"
# -n:
# -- : :
# "-f" ?
# mkdir -f # , -f mkdir ,
# mkdir -- -f -f 。
temp=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \
     -n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then echo "terminating..." >&2 ; exit 1 ; fi
# note the quotes around `$temp': they are essential!
#set , $1,$2...$n , getopt
eval set -- "$temp"
# getopt , 。
while true ; do
        case "$1" in
                -a|--a-long) echo "option a" ; shift ;;
                -b|--b-long) echo "option b, argument \`$2'" ; shift 2 ;;
                -c|--c-long)
                        # c has an optional argument. as we are in quoted mode,
                        # an empty parameter will be generated if its optional
                        # argument is not found.
                        case "$2" in
                                "") echo "option c, no argument"; shift 2 ;;
                                *)  echo "option c, argument \`$2'" ; shift 2 ;;
                        esac ;;
                --) shift ; break ;;
                *) echo "internal error!" ; exit 1 ;;
        esac
done
echo "remaining arguments:"
for arg do
   echo '--> '"\`$arg'" ;
done
実行コマンド:

./getopt.sh --b-long abc -a -c33 remain
option b, argument `abc'
option a
option c, argument `33'
remaining arguments:
--> `remain'
以上はlinux shell命令行オプションとパラメータの使い方に関する詳細です。ご協力をお願いします。