GNU/Linux >> Linux の 問題 >  >> Linux

条件付きでパイプステージをBashスクリプトに含めますか?

この質問にはすでに回答があります :条件付きパイプライン

(7つの回答)
3年前に閉鎖されました。

次のようなスクリプトがあります:

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 実行されない場合があります。これを折りたたんで、一般的なことをすべて繰り返す必要がないようにする方法はありますか?

承認された回答:

catを使用する スキップする場合は、コマンドの代わりに:

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

Linux
  1. Bashスクリプトをデバッグする方法は?

  2. テンプレートを記入するためのBashスクリプト?

  3. BashスクリプトをBashにパイプするときにパラメータを設定する方法は?

  1. URLからbashスクリプトを実行

  2. ファイルを bash シェル スクリプトに含める方法

  3. bash スクリプトをデーモンとして実行する

  1. パスに bash スクリプトを追加する

  2. Bash スクリプトのパターン マッチング

  3. jenkins パイプラインで bash コマンドを実行する