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

时间:2019-05-29 15:37:26

标签: bash

我正在尝试测试程序的输出是否等于某物,以便我可以基于它进行某事。控制台显示两个值相等,但不满足条件。

代码如下:

#!/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

请注意,此行echo "trying to get logs for test id $test_id..."仅显示"03443db8..."之类的ID,而不打印完整的句子。

echo $logecho $test_id分别在两行上记录03443db8 03443db8,因此它们应该相等。

我该如何调试?似乎有些隐藏字符是$test_id

的一部分

print_logs条件永远不会被激活,即使它们都返回等于控制台的print。如果有任何区别,我正在使用Mac。

这是我得到的奇怪输出:

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

编辑:添加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

1 个答案:

答案 0 :(得分:0)

set -x添加到脚本中,然后可以对其进行跟踪。有关更多详细信息,请参见Bash Manual: The Set Builtin。在您的情况下,脚本将是:

#!/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