1

1)

% expr "1==1"
1

2)

% expr "i==i"
invalid bareword "i"
in expression "i==i";
should be "$i" or "{i}" or "i(...)" or ...

ステップ 2 でこのエラーが発生する理由

1) % if {"i" == "i"} {
    puts "hai"
  }
  hai



2) % if {i == "i"} {
   puts "hai"
  }

invalid bareword "i"
in expression "i == "i"";
should be "$i" or "{i}" or "i(...)" or ...

if {"i" == "i"} これは if 条件での作業です。

ここで、文字列を比較せずに整数のみを評価するexprコマンドのように見つけましたが、「if」条件ではすべて(整数と文字列)が評価されています。

ここで物事はどのように機能していますか?

4

2 に答える 2

9

答えはexprマニュアルページにあります。

Operands may be specified in any of the following ways:
...
[4]    As a string enclosed in double-quotes.   The  expression  parser
       will  perform  backslash, variable, and command substitutions on
       the information between the quotes, and use the resulting  value
       as the operand

[5]    As a string enclosed in braces.  The characters between the open
       brace and matching close brace will be used as the operand with‐
       out any substitutions.
...

したがって、expr文字列を比較できますが、置換を実行するかどうかに応じて、二重引用符または中括弧で囲む必要があります。

したがって、例2では、​​使用する必要があります

% expr {"i" == "i"}

また

% expr {{i} == {i}}

文字列比較オペランドを使用することをお勧めします。

% expr {"i" eq "i"}
% expr {{i} eq {i}}

文字列の内容が数値に変換されないようにします。

于 2013-09-16T11:02:39.703 に答える