shell CSH If:“表达式语法”与值“(“进行字符串比较时出错

uyhoqukh  于 2023-08-07  发布在  Shell
关注(0)|答案(3)|浏览(135)

我有一个shell if语句如下:

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

字符串
当字符串var 1的值为**"(“**时,我得到以下错误:
if:表达式语法
我知道(是一个语法字符,我想知道是否有任何方法来比较(的字符串值

  • 谢谢-谢谢
eyh26e7m

eyh26e7m1#

使用双引号:

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

字符串

vwhgwdsa

vwhgwdsa2#

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

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

字符串
但我还是希望得到更酷的答案。- 谢谢-谢谢

hsvhsicv

hsvhsicv3#

#!/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

字符串

相关问题