csh idioms to check for environment variable existence?

∥☆過路亽.° 提交于 2019-12-08 14:28:00

问题


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.

回答1:


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.




回答2:


Try the following:

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

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


来源:https://stackoverflow.com/questions/2336388/csh-idioms-to-check-for-environment-variable-existence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!