我们遇到了一个与字符编码和 bash 替换有关的非常奇怪的难题,我们将一个 bash 变量传递到一个 rake 脚本中,该脚本被传输到另一个 bash 变量中,我不知道下一步该怎么做。它只在您以非替换方式创建变量时才有效,但不仅变量以任何方式创建时都匹配,而且它们的二进制表示也匹配。这应该排除与错误编码、空终止符或不可显示字符相关的任何内容。这是我在任何语言中见过的最有趣的错误之一,所以我想与社区分享,看看是否有人知道这里可能发生了什么。有关进一步的背景信息:
RELEASE_CODE=1250
MATCHING=replace
NAME=redactedDeployment-replace-d
首先,脚本通过并产生预期结果的工作案例。
# FULLNAME=redactedDeployment-1250-d
# deployments=$(rake find_deployments[$FULLNAME])
# echo $deployments
redactedDeployment-1250-d
# echo $FULLNAME
redactedDeployment-1250-d
# echo $FULLNAME | perl -lpe '$_=join " ", unpack"(B8)*"'
01110010 01100101 01100100 01100001 01100011 01110100 01100101 01100100 01000100 01100101 01110000 01101100 01101111 01111001 01101101 01100101 01101110 01110100 00101101 00110001 00110010 00110101 00110000 00101101 01100100
和
# if [ "${MATCHING,,}" = "replace" ]; then
> FULLNAME=${NAME/replace/$RELEASE_CODE}
> tr -dc '[[:print:]]' <<< "$FULLNAME" #Remove non-printable characters, same result if we remove this line
> else
> FULLNAME=${NAME}-${RELEASE_CODE}
> fi
# deployments=$(rake find_deployments[$FULLNAME])
rake aborted!
Don't know how to build task 'find_deployments[redactedDeployment-1250-d' (See the list of available tasks with `rake --tasks`)
(See full trace by running task with --trace)
# echo $FULLNAME
redactedDeployment-1250-d
# echo $FULLNAME | perl -lpe '$_=join " ", unpack"(B8)*"'
01110010 01100101 01100100 01100001 01100011 01110100 01100101 01100100 01000100 01100101 01110000 01101100 01101111 01111001 01101101 01100101 01101110 01110100 00101101 00110001 00110010 00110101 00110000 00101101 01100100