我正在用一些为我生成一些环境变量的bash脚本。我在 .travis.yml 文件中使用它。
我的加密密钥看起来像:
someRandomCharacters
withNewLine
在终端我检查了三种可能性。
echo "someRandomCharacters
withNewLine" | openssl enc -aes-128-cbc -a -salt -pass pass:SomePassword -base64 -d
和
echo "someRandomCharactersnWithNewLine" | openssl enc -aes-128-cbc -a -salt -pass pass:SomePassword -base64 -d
会给我正确的输出。
echo "someRandomCharactersWithNewLine" | openssl enc -aes-128-cbc -a -salt -pass pass:SomePassword -base64 -d
上面的这个将返回error reading input file
到目前为止,一切都很好 - 我知道为什么它会这样。但是,当我尝试输入任何上述选项时 - 例如这样:
- SOME_ENV=`echo "someRandomCharactersnWithNewLines" | openssl enc -aes 128-cbc -a -salt -pass pass:SomePassword -base64 -d`
进入 travis.yml ,两个 last 选项将返回error reading input file
,并且第一个将由于不正确的而崩溃整个构建.yaml 语法。
我尝试使用以上这三个中的任何一个 其他更多内容,例如" n" 作为特殊字符,如我在Sto上的示例中所发现的那样。他们中的任何一个都会返回error reading input file
,他们都没有将我解密的SOME_ENV
返回到Travis。有什么解决方案吗?或者,也许我对Bash和Yaml的糟糕经历阻止了我看到明显的错误?
虽然很难确切地判断出问题是什么问题,但这里有一些数据点:
dash
(如果您只在现代Linux发行版上运行sh
,您会得到的外壳)和bash
在某些情况下的行为会有所不同。
您绝不应该假设代码片段使用bash
运行,因为许多原因都可能导致使用sh
运行,有时很难说。
这是一个包含n
序列的脚本,可与sh
一起使用,但与bash
失败:
$ cat myfile
echo "U2FsdGVkX19EB+D8non9+9bnl4dE5H2WbOUSvsGZjK7s=" | openssl enc -aes-128-cbc -a -salt -pass pass:MyPassword -base64 -d
$ sh myfile
My test data
$ bash myfile
error reading input file
如果我们使用echo -e
,则在dash
失败和bash
工作的情况下得到相反的结果:
$ cat myfile
echo -e "U2FsdGVkX19EB+D8non9+9bnl4dE5H2WbOUSvsGZjK7s=" | openssl enc -aes-128-cbc -a -salt -pass pass:MyPassword -base64 -d
$ sh myfile
error reading input file
$ bash myfile
My test data
这就是为什么POSIX建议不使用echo
的原因。如果我们相反使用printf
,则可以在这两者上使用:
$ cat myfile
printf "U2FsdGVkX19EB+D8non9+9bnl4dE5H2WbOUSvsGZjK7s=n" | openssl enc -aes-128-cbc -a -salt -pass pass:MyPassword -base64 -d
$ sh myfile
My test data
$ bash myfile
My test data
但是,中间的线馈序是openssl
的可选,并且可以删除(即使您似乎说这不起作用:也许您删除了,但不是
n
?)
$ cat myfile
echo "U2FsdGVkX19EB+D8no9+9bnl4dE5H2WbOUSvsGZjK7s=" | openssl enc -aes-128-cbc -a -salt -pass pass:MyPassword -base64 -d
$ sh myfile
My test data
$ bash myfile
My test data