csh习惯用来检查环境变量的存在? [英] csh idioms to check for environment variable existence?

查看:529
本文介绍了csh习惯用来检查环境变量的存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个csh脚本,我需要检查某些环境变量是否在我开始做某些事情之前设置,所以我做这样的事情:

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?

注意:

  • 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.

推荐答案

我认为你这样做的方式(一个如果语句,条件是使用 $?VAR 语法,如果变量设置为1,则评估为1)否则为0)可能是最惯用的 csh 构造,可以执行所需的操作。

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.

这篇关于csh习惯用来检查环境变量的存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆