パイプラインを介してcsh / tcshに入力すると入力されます。

パイプラインを介してcsh / tcshに入力すると入力されます。

2つのパイプ間の変数をテストするcshスクリプトが必要です。たとえば(jsonファイルのようなものを渡しますが、改行やインデントなし):

set debug = 1
cat test.inp | if ($debug) then (jq '.response' | tee test2.json) else python -m json.tool endif > test.output

デバッグ変数の値が何であれ、出力はに保存されるというアイデアですtest.output。ただし、次のように書くとエラーが発生します。

if:それでは不適切です。

csh / tcshでこれを行う方法はありますか(sh / bashにあるように見えます)?

ベストアンサー1

Cシェルから制御構造にパイプするのはエラーです。制御構造の配管ソリューションは、スクリプトまたはFIFOファイルを生成することです。以下はサンプルファイルです。

echo '\
# Functions\
alias function '\''( set argv = ( \\\!* ) ; source ~/.cshfuncs )'\' >> ~/.cshrc
echo 'switch ("$1")\
  case 'myfunc':\
    shift\
    if ("$1") then\
      ( jq '.response' | tee test2.json )\
    else\
      python -m json.tool\
    endif\
    exit\
  default:\
    echo "Function $1 not set."\
    exit\
endsw' > ~/.cshfuncs

これはFIFOの例です。

mkfifo ~/fifo
echo 'if ("$?debug") then\
( jq '\''.response'\'' | tee test2.json )\
else\
python -m json.tool\
endif' > ~/fifo & cat test.inp | ( set debug ; source ~/fifo ) > test.output

おすすめ記事