bashスクリプトに条件付きパイプラインステップを含める

bashスクリプトに条件付きパイプラインステップを含める

次のスクリプトがあります。

flag=false
# Do a bunch of stuff that might set flag to true.
if [[ "$flag" == "true" ]]; then
   command \
       | pipe_command_a \
       | pipe_command_b \
       | pipe_command_c \
       | pipe_command_d \
       > "${output_path}"
else
   command \
       | pipe_command_a \
       | pipe_command_c \
       | pipe_command_d \
       > "${output_path}"
fi

flag存在するものと作成することのtrue唯一の違いは、それが実行されない可能性があることfalseです。pipe_command_b一般的なことをすべて繰り返す必要がないように縮小する方法はありますか?

ベストアンサー1

スキップするには、cat代わりに次のコマンドを使用します。

command=cat
if [[ $flag == true ]] ; then
    command=pipe_command_b
fi
command \
    | pipe_command_a \
    | $command       \
    | pipe_command_c

おすすめ記事