如何在.csh脚本中进行三元运算

Tus*_*arg 3 csh

我想要做

a = $b == howru ? good : not_good;
Run Code Online (Sandbox Code Playgroud)

在c shell脚本中

我尝试使用&&and||运算符,但似乎我犯了一些错误,因为我习惯在 Bash 上工作。

Mar*_*oij 5

首先,你的语法是错误的:

  1. 您需要分配变量set
  2. ?..:不是 csh 中的运算符。

“正常”的csh if声明如下:

if ( cond ) then
  foo
else
  bar
endif
Run Code Online (Sandbox Code Playgroud)

你实际上可以缩短if

if ( cond ) foo
Run Code Online (Sandbox Code Playgroud)

else但据我所知,你不能在这里添加。

“短”语法的工作方式与 bourne shell 中完全相同,只是您需要使用关键字set

% [ "howru" = 'howru' ] && set a = good || set a = not_good
% echo $a
good

% [ "XXhowru" = 'howru' ] && set a = good || set a = not_good
% echo $a
not_good
Run Code Online (Sandbox Code Playgroud)