空の文字列がコマンドラインを破る

空の文字列がコマンドラインを破る

私の問題を説明できません。ご了承ください。コマンドを呼び出すスクリプトがあります。入力ファイルの選択に応じて、コマンドラインに追加のパラメータを含める必要があります。私はこれを試しました:

    case "$model" in
    CNRM-CM6-1|CNRM-ESM2-1)
        trim_op="-selindexbox,2,361,2,293"
    ;;
    EC-Earth3)
        trim_op="-selindexbox,2,361,2,291"
    ;;
    IPSL-CM6A-LR)
        trim_op="-selindexbox,2,361,2,331"
    ;;
    MPI-ESM1-2-HR)
        trim_op="-selindexbox,2,801,3,403"
    ;;
    MPI-ESM1-2-LR)
        trim_op="-selindexbox,2,255,2,219"
    ;;
    NorESM2-LM)
        trim_op="-selindexbox,2,359,2,383"
    ;;
    *)
        trim_op=""
    ;;
esac

cdo -O remapcon,"$target_grid" "$trim_op" "$input_file" "$output_file"

しかし、バッシュはその空の言葉に喉が詰まった。 Bashでこれを行う正しい方法は何ですか?私がしたことは次のとおりです。

if [[ -z $trim_op ]] ; then
    cdo -O remapcon,"$target_grid" "$input_file" "$output_file"
else
    cdo -O remapcon,"$target_grid" "$trim_op" "$input_file" "$output_file"
fi

今はとても無知だと思います。これは名前がありますか?私が検索するたびに表示されます頂上に登るこれは私が探しているものではありません。

ベストアンサー1

変数が空のとき"$trim_op"に呼び出しからパラメータを削除するには、次のようにします。cdo

cdo -O remapcon,"$target_grid" ${trim_op:+"$trim_op"} "$input_file" "$output_file"

変数拡張${trim_op:+"$trim_op"} 次に展開 "$trim_op"(その後、さらに拡張されます)trim_op変数が設定され、変数の値が空でない場合。

おすすめ記事