比较长字符串和空格

时间:2016-03-19 23:04:55

标签: bash

我试图比较两个字符串,这与我在调试下一行的日志中看到的相同:

if ["$line" ne "${fingerPrints[$i]}"]; then

但是当我在比较这些字符串后尝试一行时,

33 + '[DCD0 5B71 EAB9 4199 527F 44AC DB6B 8C1F 96D8 BF6D' ne
'DCD0 5B71 EAB9 4199 527F 44AC DB6B 8C1F 96D8 BF6D]' ./gentoo-stage.sh:

line 33: [DCD0 5B71 EAB9 4199 527F 44AC DB6B 8C1F 96D8 BF6D: command not found

这就是发生的事情:

!=

我不是要检查一个字符串是否包含另一个字符串,只要它们是euqal。

当我尝试ne而不是transform-orgin时,结果相同。

1 个答案:

答案 0 :(得分:2)

您必须在[之后和]之前添加空格。所以而不是:

if ["$line" ne "${fingerPrints[$i]}"]; then

像这样写:

if [ "$line" ne "${fingerPrints[$i]}" ]; then

“ne”不是运营商。你可能意味着!=

if [ "$line" != "${fingerPrints[$i]}" ]; then

Btw -ne用于比较整数表达式,并且您正在比较字符串,因此!=实际上是唯一有意义的运算符。