我有一个scala项目,它在build.sbt文件中定义了它的版本。
我想在bash脚本中提取该版本,以使用它来构建jar文件的路径,然后我想将其上传到S3。
然而,aws s3 cp命令不起作用。我发现当路径被单引号包围时它不起作用,但是当路径没有任何引号时它确实有效。例如:
aws s3 cp '<path_to_the_jar>' 's3://<bucket>/<folder>/jar_file'
但它没有路径旁边的单引号:
aws s3 cp <path_to_the_jar> s3://<bucket>/<folder>/jar_file
我这样做的方式:
~ sbt version
产生
[info] Loading global plugins from /Users/myuser/.sbt/0.13/plugins
[info] Updating {file:/Users/myuser/.sbt/0.13/plugins/}global-plugins...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Loading project definition from /Users/myuser/IdeaProjects/MY-PROJECT
[info] Updating {file:/Users/myuser/IdeaProjects/MY-PROJECT
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to MY-PROJECT-build (in build file:/Users/myuser/IdeaProjects/MY-PROJECT)
[info] MY-PROJECT-bindings/*:version
[info] 0.0.0
[info] MY-PROJECT/*:version
[info] 0.0.0
[info] {.}/*:version
[info] 0.1-SNAPSHOT
我使用sed
处理它,这给了我想要的版本:
~ sed -n "/MY-PROJECT\/\*:version/{n;p;}" | cut -f 2
0.0.0
整个脚本:
script.sh
#!/usr/bin/env bash
set -e
set -x
example="MY-PROEJCT"
s3_bucket="BUCKET"
s3_folder="TEST"
# problematic variable, gets set to ‘0.0.0’ instead of just 0.0.0
version=$(sbt version | sed -n "/MY-PROJECT\/\*:version/{n;p;}" | cut -f 2)
# Builds the jar file locally
~ sbt assembly
# The command is expanded to: JAR_S3_PATH='s3://BUCKET/TEST/MY-PROJECT-test-0.0.0.jar'
JAR_S3_PATH=s3://$s3_bucket/$s3_folder/$example-test-$version.jar
echo $example
echo $version
echo $JAR_S3_PATH
# The command is expanded to: aws s3 cp '/target/scala-2.11/MY-PROJECT-test-assembly-0.0.0.jar' 's3://BUCKET/TEST/MY-PROJECT-test-0.0.0.jar'
aws s3 cp /target/scala-2.11/$example-test-assembly-$version.jar ${JAR_S3_PATH}
最终aws s3 cp
命令不起作用。奇怪的是,如果我只是将其粘贴到终端中它确实有效,但它不在脚本中。
我确实在bash中研究过单引号和双引号之间的区别,但我仍然不知道这对我的问题有何影响。
修改
当我使用echo
打印时,所有变量都不包含引号。但是,为此脚本设置了选项set -x
,它会打印脚本完成的所有操作。请看这个:
+ echo MY-PROJECT
MY-PROJECT
+ echo '0.0.0'
0.0.0
+ echo 's3://BUCKET/TEST/MY-PROJECT-test-0.0.0.jar'
s3://here-avf-shared/emr_test/tiledgeo-validator-emr-assembly-0.0.0.jar
正如您可以看到执行echo $example
会导致echo MY-PROJECT
,但执行echo $version
会导致echo '0.0.0'
。如果我只是将版本硬编码为:
version="2.11"
然后结果就是:
+ echo MY-PROJECT
MY-PROJECT
+ echo 0.0.0
0.0.0
+ echo s3://BUCKET/TEST/MY-PROJECT-test-0.0.0.jar
s3://here-avf-shared/emr_test/tiledgeo-validator-emr-assembly-0.0.0.jar
请注意,0.0.0
周围没有单引号!我不知道为什么会这样。也许是因为我将sed
和cut
的结果分配给version
变量,这使得它的行为有所不同..
编辑2
Gordon Davisson说version
变量中还有其他内容。
++ sbt version
++ sed -n '/MY-PROJECT\/\*:version/{n;p;}'
++ cut -f 2
+ version='0.0.0'
+ echo '<0.0.0>'
+ LC_ALL=C
+ cat -vt
<0.0.0^[[0m>