简体   繁体   English

2个相同的字符串在bash中不相等

[英]2 same string are not equal in bash

I'm trying to test if the output of a program is equal to something, so that I can do something based on it. 我试图测试一个程序的输出是否等于某个东西,以便我可以根据它做一些事情。 The console prints that the 2 values are equal, but the condition is not met. 控制台打印出2个值相等,但不满足条件。

Here's the code: 这是代码:

#!/bin/bash

test_id=`clasp run testRunner`

function get_logs() {
  echo "trying to get logs for test id $test_id..."

  logs=`clasp logs`

  if logs_contain_test_id
  then
    print_logs
  else
    get_logs
  fi
}

function logs_contain_test_id() {
  IFS=$' '
  for log in $logs
  do
    echo $log
    echo $test_id
    if [[ "$log" == "$test_id" ]]
    then
      return 0
    fi
  done
  return 1
}

get_logs

Note that this line echo "trying to get logs for test id $test_id..." only prints the id like "03443db8..." and not the full sentence. 请注意,此行echo "trying to get logs for test id $test_id..."仅打印I​​D,如"03443db8..."而不是完整的句子。

Also the echo $log and echo $test_id log 03443db8 03443db8 on 2 separate lines so they should be equal. 还有echo $logecho $test_id log 03443db8 03443db8在2个单独的行上,所以它们应该相等。

How can I debug this? 我该怎么调试呢? Seems like some hidden characters are part of $test_id 好像一些隐藏的字符是$test_id一部分

The condition print_logs never gets activated, even if both returns print equal to the console. 条件print_logs永远不会被激活,即使两者都返回print等于控制台。 I am on a mac if it makes any difference. 如果它有任何区别,我在Mac上。

Here's the strange output I am getting: 这是我得到的奇怪输出:

tests,
03443db8
0
03443db8
failures,
03443db8
03443db8
03443db8
03443db8... <-- this should be "trying to get logs for test id 03443db8..."

edit: after adding set -x I get these logs: 编辑:添加set -x我得到这些日志:

+ [[ failures, == \[\2\K\[\1\G\4\4\f\a\7\0\c\7 ]]
+ for log in '$logs'
+ echo 44fa70c7
44fa70c7
44fa70c7'
44fa70c7
+ [[ 44fa70c7 == \[\2\K\[\1\G\4\4\f\a\7\0\c\7 ]]
+ return 1
+ get_logs
44fa70c7...'
44fa70c7...
++ clasp logs

Add set -x to your script then you can trace it. set -x添加到脚本中,然后就可以跟踪它了。 See Bash Manual: The Set Builtin for more details. 有关详细信息,请参阅Bash手册:Set Builtin In your case the script would be: 在您的情况下,脚本将是:

#!/bin/bash
set -x
test_id=`clasp run testRunner`

function get_logs() {
  set -x
  echo "trying to get logs for test id $test_id..."

  logs=`clasp logs`

  if logs_contain_test_id
  then
    print_logs
  else
    get_logs
  fi
}

function logs_contain_test_id() {
  set -x
  IFS=$' '
  for log in $logs
  do
    echo $log
    echo $test_id
    if [[ "$log" == "$test_id" ]]
    then
      return 0
    fi
  done
  return 1
}

get_logs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM