C shell 脚本中是否有短路逻辑运算符?
我认为 C shell 脚本的行为会像 C 一样,并对逻辑运算符使用短路求值。
if ((! -e $cache ) || ("`find $monitor -newer $cache`" != "")) then
...
endif
但在 if 语句中,即使第一个条件为真,也会检查第二个条件,从而给出错误。
C shell 脚本中是否有短路逻辑或?
I thought C shell script will behave like C and use short circuit evaluation for logical operators.
if ((! -e $cache ) || ("`find $monitor -newer $cache`" != "")) then
...
endif
But in the if statement, even if the first condition is true, the second is checked giving me errors.
Do we have a short circuit logical OR in C shell script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,
&&
和||
是 tcsh 中的短路运算符(就像它们在 C 中一样)。有两种不同的形式,一种适用于命令(如false && echo foo
),另一种适用于表达式(如if (-e $file1 & ;& -e $file2)
)。我不完全确定这里发生了什么,除了 csh 和 tcsh 因语法定义不明确而臭名昭著。一些实验表明,将
find
的输出保存在变量中可以避免错误:(请注意,额外的括号并不是真正必要的,但它们不会造成任何损害。)
但更多的改进是可能的。将 find 命令的整个输出存储在变量中并不是真正必要的。相反,您可以调用 find 并使用其返回的状态来告诉您是否找到了任何内容:
我刚刚尝试过:
并且它有效 - 除了
>& /dev/null
重定向似乎在大括号内被忽略。相反,您可以单独执行“find”命令,然后检查生成的 $status。这意味着您失去了短路
||
运算符的好处;您必须求助于嵌套的if
语句和/或临时变量。也许真正的教训是这样的:我使用 csh 和 tcsh 的时间比我愿意承认的要长,而我能够弄清楚很多这些东西的唯一方法就是通过反复试验。您可以使用 tcsh 或 csh 来完成此操作,但最好使用不同的脚本语言。例如,在 Bourne shell 中,这相当简单:
Yes,
&&
and||
are short-circuit operators in tcsh (just as they are in C). And there are two distinct forms, one that works on commands (as infalse && echo foo
), and one that works on expression (as inif (-e $file1 && -e $file2)
).I'm not entirely sure what's going on here, except that csh and tcsh are notorious for ill-defined syntax. A bit of experimentation shows that saving the output of
find
in a variable avoids the error:(Note that the extra parentheses aren't really necessary, but they don't hurt anything.)
But more improvement is possible. Storing the entire output of the find command in a variable isn't really necessary. Instead, you can invoke find and use its returned status to tell you whether it found anything:
I just tried this:
and it worked -- except that the
>& /dev/null
redirection seems to be ignored within the braces.Instead, you can execute the "find" command separately, and then examine the resulting $status. Which means that you lose the benefit of the short-circuit
||
operator; you'll have to resort to nestedif
statements and/or temporary variables.Perhaps the real lesson is this: I've been using csh and tcsh for more years than I care to admit, and the only way I was able to figure out a lot of this stuff was by trial and error. You can do this using tcsh or csh, but you're probably better of using a different scripting language. For example, in Bourne shell, this is fairly straightforward:
通常,
&&
和||
是短路的。考虑这样的事情:在这两种情况下, foo 都不会被输出。
但是,据我所知,您不能像这样使用这种字符串比较,即使短路,csh 仍然会对整个事情进行语法检查。
Usually,
&&
and||
are short-circut. Consider something like this:In both cases, foo won't be put out.
But, AFAIK you cannot use this kind of string comparison like this, even if short-circuit, csh will still syntax-check the whole thing.