代替オプションフィールド

代替オプションフィールド

スクリプトには、ユーザー入力が必要なオプションのリストがあります。

彼ら

-c cell name->必ずそこにいるでしょう

-n node->必ずそこにいるでしょう

-s server->必ずそこにいるでしょう

ここまではすべてが正常です。ここまでのコードは次のとおりです。while getopts c:n:s:

問題はここから始まります

および/または条件に入力する必要がある2つの異なるフィールドがあります。

-i initial heap size

-m max heap size

ユーザーはオプションの一方または両方を入力します。

現在私は次のようなものを持っています

#get the arguments -c for cell, -n for node, -s for server, -i for initial heap, -m for max heap
while getopts c:n:s:im opt; do
        case $opt in
          c)
                CELL=$OPTARG
                ;;
          n)
                NODE=$OPTARG
                ;;
          s)
                SERV=$OPTARG
                ;;
          i)
                INI_HEAP=$OPTARG
                ;;
          m)
                MAX_HEAP=$OPTARG
                ;;
          ?)
                echo "a.sh -c <CELL> -n <NODE> -s <SERVER> [-i <INITAIL HEAP> | -m <MAX HEAP>"
                ;;
        esac
done

#test if cell name is not null
if ! test "$CELL" || ! test "$NODE" || ! test "$SERV" ; then
        echo "a.sh -c <CELL> -n <NODE> -s <SERVER> [-i <INITAIL HEAP> | -m <MAX HEAP>]"
        exit 1
fi

#test if both initial heap and max heap are null
flag=0
if ! test "$INI_HEAP" ; then
        if ! test "$MAX_HEAP" ; then
                flag=1;
        fi
fi

if [[ $flag -eq 1 ]] ; then
        echo "flag a.sh -c <CELL> -n <NODE> -s <SERVER> [-i <INITAIL HEAP> | -m <MAX HEAP>]"
        exit 1
fi

#test for non numeric value of initial heap size
if [[ "$INI_HEAP" == +([0-9]) ]] ; then
        continue
else
        echo "num a.sh -c <CELL> -n <NODE> -s <SERVER> [-i <INITAIL HEAP> | -m <MAX HEAP>]"
        exit 1
fi

-i機能とオプションを実装するにはどうすればよいですか-m

ベストアンサー1

変数$flagが重複しています。変数を設定してすぐにテストします。最初に設定するのではなく、エコーをするだけです。したがって、ロジックは次のようになります。

  • キャプチャオプション/optargs
  • CELL、NODE、SERVERがすべて設定されていることを確認してください。
  • INI_HEAPまたはMAX_HEAPが設定されていてintであることを確認してください。
#get the arguments -c for cell, -n for node, -s for server, -i for initial heap, -m for max heap
while getopts c:n:s:i:m: opt; do
        case $opt in
          c)
                CELL=$OPTARG
                ;;
          n)
                NODE=$OPTARG
                ;;
          s)
                SERV=$OPTARG
                ;;
          i)
                INI_HEAP=$OPTARG
                ;;
          m)
                MAX_HEAP=$OPTARG
                ;;
          ?)
                echo "Usage: a.sh -c <CELL> -n <NODE> -s <SERVER> [-i <INITIAL HEAP> | -m <MAX HEAP>]"
                exit 1
                ;;
        esac
done

#test if cell name is not null
if [[ -z "$CELL" || -z "$NODE" || -z "$SERV" ]]; then
    echo "Cell, Node and Server are mandatory values"
    echo "Usage: a.sh -c <CELL> -n <NODE> -s <SERVER> [-i <INITIAL HEAP> | -m <MAX HEAP>]"
    exit 1
fi

#make sure either -i or -m was used (or both) and is an integer
shopt -s extglob
if [[ ( -z "$INI_HEAP" && -z "$MAX_HEAP" ) || -n "${INI_HEAP##+([0-9])}" || -n "${MAX_HEAP##+([0-9])}" ]]; then
    echo "Initial heap size or maximum heap size (or both) must be specified, and must be an integer"
    exit 1
fi
shopt -u extglob

おすすめ記事