【问题标题】:Shell/Bash - equality check between strings returns 1 on matchShell/Bash - 字符串之间的相等性检查在匹配时返回 1
【发布时间】:2021-11-01 22:22:14
【问题描述】:

我正在尝试将 cli 命令 "successful" 的输出与简单的 test = $? 进行比较,但即使值匹配,也会得到 1。我在硬编码示例上做了同样的事情并且工作正常。

存储 cli 输出的变量是否应该使用不同的包装?

#ri status:
ri_status=`aws dms describe-connections --filter Name=replication-instance-arn,Values=$ri_arn --query=Connections[0].Status --region us-east-1`

#returns "successful"
echo $ri_status

#this returns 0
test "successful" = "successful"
echo $?

#this returns 1
test $ri_status = "successful"
echo $?

【问题讨论】:

  • 在命令返回的末尾可能是\n
  • 试试test "$ri_status" = "successful"
  • 收到1 Paolo。不过,感谢 4 位帮助!
  • 您确定$ri_status 完全“成功”了吗?它可能包含空格(echo $ri_status 将删除)、非打印/不可见字符(如回车符)等。试试declare -p ri_status | LC_ALL=C cat -v 看看是否有任何奇怪的东西出现。
  • 在线上方的位置? declare -p ri_status | LC_ALL=C cat -v

标签: bash amazon-web-services shell command-line-interface


【解决方案1】:

'修复

http://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Quoting

3.1.2.2 单引号

用单引号 (''') 括起来的字符会保留文字 引号内每个字符的值。单引号不能 出现在单引号之间,即使前面有反斜杠。

test $ri_status = '"successful"'

【讨论】:

    猜你喜欢
    • 2019-08-29
    • 2022-07-07
    • 1970-01-01
    • 2019-12-25
    • 2015-10-23
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多