csh idioms to check for environment variable existence?
Asked Answered
C

2

31

I've got a few csh scripts where I need to check that certain environment variables are set before I start doing stuff, so I do this sort of thing:

if ! $?STATE then
    echo "Need to set STATE"
    exit 1
endif

if ! $?DEST then
    echo "Need to set DEST"
    exit 1
endif

which is a lot of typing. Is there a more elegant idiom for checking whether or not an environment variable is already set?

Notes:

  • This question is quite similar, but specifically asks about solutions in bash.
  • I'm not looking for people to advise me to stay away from csh because it's cursed, scary, or bash is better. I'm specifically interested in a more elegant solution than what I'm using now.
Carriole answered 25/2, 2010 at 18:1 Comment(1)
This newer, similar question also show how to check in an expression context where if/else/endif is not possibleBrainless
K
22

I think the way you're doing it (an if statement with a condition using the $?VAR syntax, which evaluates to 1 if the variable is set, and 0 otherwise) is probably the most idiomatic csh construct that does what you want.

Kallman answered 25/2, 2010 at 18:19 Comment(0)
T
-7

Try the following:

[ -z STATE ] && echo "Need to set STATE"

[ ! -z DEST  ] && echo "Need to set STATE"
Triphylite answered 28/8, 2014 at 15:53 Comment(2)
It's not clear what you're trying to say here - you have two opposite pieces of logic printing the same message.Raeannraeburn
In any case this is not cshLonesome

© 2022 - 2024 — McMap. All rights reserved.