CSH 如果:与值“(”进行字符串比较时出现“表达式语法”错误

问题描述 投票:0回答:3

我有一个 shell if 语句如下:

if($var1!= $var2) then
    ...  
endif

当字符串 var1 的值为 "(" 时,出现以下错误:

if:表达式语法

我知道 ( 是一个语法字符,我想知道是否有任何解决方法可以比较字符串值 (

谢谢。

shell syntax scripting syntax-error csh
3个回答
1
投票

使用双引号:

if ("$var1" != "$var2") then
  ... 
endif

1
投票

最后我找到了一个非常间接的解决方案来解决这个问题:

if(`echo $var1` != `echo $var2`) then
    ...  
endif

但我仍然希望收到更酷的答案。谢谢。


0
投票
     #!/bin/tcsh
     set A="a"
     set B="b"
     echo A
     echo B
     echo $A
     echo $B
     echo ${A}
     echo ${B}
     #
     if ( `echo $A` == `echo $B` ) then
     pwd
     else
     date
     endif
     #
     echo " simpler  alternative, i.e. A instead of $A, etc. "
     if ( `echo A` == `echo B` ) then
     pwd
     else
     date
     endif
© www.soinside.com 2019 - 2024. All rights reserved.