I would like to have a csh script to test a variable between two pipes. For example (the incoming resembles a json file but without the newlines and indentations):

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

The idea is that whatever the value of the variable debug, the output is saved in test.output. But the way I wrote this leads to the error:

if: Improper then.

Is there any way to do this in csh/tcsh (as there seems to be in sh/bash)?

  • In bash at least the parentheses would not be necessary around the jq part, but you would need a semi-colon after ($debug)—seems like you need to check csh’s grammar or switch to bash – D. Ben Knoble Dec 30 '19 at 14:12
  • 2
    @D.BenKnoble bash syntax is almost entirely unlike that of the csh shells. Don't try applying one to the other. – roaima Dec 30 '19 at 14:13
  • @roaima agreed, wasn’t trying to. Just spotting differences, and suggesting that grasping the csh grammar would solve the problem. I know bash intimately; csh, hardly at all. – D. Ben Knoble Dec 30 '19 at 14:28
  • A related question is unix.stackexchange.com/q/468284/5132 . – JdeBP Dec 30 '19 at 14:30
  • Thanks to both Ben and roaima for spotting two csh mistakes, 1) the semi-column and 2) the missing endif. These are now corrected in the edited version of the post. – gammarayon Dec 30 '19 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.