比较字符串的 Linux 脚本错误

标签 linux bash unix string-comparison string-length

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

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

    if [ "$name_1" -eq  "$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 $(1)"            
    echo "String 2 length is $(2)"

    done

最佳答案

积分:

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

  • -eq 做算术比较;在比较字符串时使用 = (POSIX) 或 ==

关于比较字符串的 Linux 脚本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36779073/

相关文章:

linux - 匹配文本和语音

c++ - 在不禁用 cout/cin 的情况下获取标准输出内容

linux - 如何显示文件中bash shell的所有命令? Linux

regex - bash - 如何从 bash 脚本中将换行符传递给 perl?

bash - 后台作业的乱码输出

arrays - 从 bash 到 ash shell : how to handle arrays defined by input? 的翻译

linux - 在 Unix 中寻找集合补码

php - Python 将数据 POST 到 Linux 上的本地 PHP

string - 如何将字符串转换为 Bash 中的所有空格?

linux - 我如何正确引用此 bash 管道以供观看?