比较字符串的Linux Script错误

时间:2016-04-21 19:44:23

标签: linux bash unix string-comparison string-length

我正在尝试接受两个字符串输入并检查它们是否相等,如果不相等则告诉字符长度。我键入两个字符串后,我找不到错误,有人能说出我做错了什么吗?我尝试使用:

WHERE b.machine_id = 1

3 个答案:

答案 0 :(得分:1)

这个将完全按预期工作:

#!/bin/bash
while true; do
echo "Please Enter two name to compare"
read name_1 name_2
one=${#name_1}
two=${#name_2}

if [ "$name_1" = "$name_2" ]
then
  echo "$name_1 and $name_2 are equal"
else
  echo "$name_1 and $name_2 are not equal"
fi 
echo "String 1 length is $one"            
echo "String 2 length is $two"

done

答案 1 :(得分:1)

点数:

  • bash中的用户定义变量不能以数字开头,因此显然不能只是数字

  • -eq进行算术比较;当您使用=(POSIX)或==

  • 比较字符串时

答案 2 :(得分:0)

尝试以下方法。我在[[ ]]周围使用了if,并将变量从12更改为xy

#!/bin/bash
while true; do
    echo "Please Enter two name to compare"
    read name_1 name_2
    x=${#name_1}
    y=${#name_2}

    if [[ "$name_1" == "$name_2" ]]
    then
      echo "$name_1 and $name_2 are equal"
    else
      echo "$name_1 and $name_2 are not equal"
    fi
    echo "String 1 length is $x"
    echo "String 2 length is $y"

done
exit