パラメータリストにパラメータを追加する

パラメータリストにパラメータを追加する

次のBashコードがあります。

function suman {

    if test "$#" -eq "0"; then
        echo " [suman] using suman-shell instead of suman executable.";
        suman-shell "$@"
    else
        echo "we do something else here"
    fi

}


function suman-shell {

    if [ -z "$LOCAL_SUMAN" ]; then
        local -a node_exec_args=( )
        handle_global_suman node_exec_args "$@"
    else
        NODE_PATH="${NEW_NODE_PATH}" PATH="${NEW_PATH}" node "$LOCAL_SUMAN" --suman-shell "$@";
    fi
}

ユーザーがパラメーターなしでコマンドを実行すると、suman以下が発生します。

  echo " [suman] using suman-shell instead of suman executable.";
  suman-shell "$@"

私の質問は - "$ @"値にパラメータをどのように追加しますか?単に次のようなことをするだけです。

handle_global_suman node_exec_args "--suman-shell $@"

どうやらこれは間違っていますが、どうすればいいのかわかりません。私は何ですかいいえ探す -

handle_global_suman node_exec_args "$@" --suman-shell

問題はそれがうまくいくということです。handle_global_suman私が入ったら、別のコードを変更する必要があり、むしろそれを避けたいと思います。$1$2--suman-shell$3

予備の回答:

    local args=("$@")
    args+=("--suman-shell")

    if [ -z "$LOCAL_SUMAN" ]; then
        echo " => No local Suman executable could be found, given the present working directory => $PWD"
        echo " => Warning...attempting to run a globally installed version of Suman..."
        local -a node_exec_args=( )
        handle_global_suman node_exec_args "${args[@]}"
    else
        NODE_PATH="${NEW_NODE_PATH}" PATH="${NEW_PATH}" node "$LOCAL_SUMAN" "${args[@]}";
    fi

ベストアンサー1

パラメータを配列に入れ、配列に追加します。

args=("$@")
args+=(foo)
args+=(bar)
baz "${args[@]}"

おすすめ記事