Bashのユーティリティの連鎖呼び出しのパラメータ化

Bashのユーティリティの連鎖呼び出しのパラメータ化

私はstdinからデータ列を読み取り、それを処理(平滑化効果を適用)してからstdoutに出力するBashシェルで使用されるブラックボックスUNIXプログラムを持っています。たとえば、UNIXパイプを介して使用します。

generate | smooth | plot  

さらにスムーズにするには、スムージングを繰り返してBashコマンドラインから呼び出すことができます。

generate | smooth | smooth | plot   

でも

generate | smooth | smooth | smooth | smooth | smooth | smooth | smooth | smooth | smooth | smooth | plot

これはますます不快になりました。smooth出力をsmooth新しいインスタンスに複数回パイプできるようにするBashラッパーを作成したいと思います。

generate | newsmooth 5 | plot

変える

generate | smooth | smooth | smooth | smooth | smooth | plot

私の最初の試みは、Bashスクリプトを使用して現在のディレクトリに一時ファイルを作成して削除することでしたが、書き込みアクセス権があるディレクトリにない場合は見苦しく、文書が破損してもゴミも残ります。

このプログラムにはパラメータはありませんsmooth

呼び出し回数をパラメータ化するために、これらのプログラムを「ラップ」するよりエレガントな方法はありますか?

ベストアンサー1

再帰関数でラップできます。

smooth() {
  if [[ $1 -gt 1 ]]; then # add another call to function
    command smooth | smooth $(($1 - 1)) 
  else
    command smooth # no further 
  fi
}

次のように使用できます。

generate | smooth 5 | plot

これは次のとおりです。

generate | smooth | smooth | smooth | smooth | smooth | plot

おすすめ記事