Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux bash表达式的结果无法解释为字符串_Linux_Bash_Curl - Fatal编程技术网

Linux bash表达式的结果无法解释为字符串

Linux bash表达式的结果无法解释为字符串,linux,bash,curl,Linux,Bash,Curl,正在尝试从GitHub下载最新SBT版本: version=“$(curl-vsLkhttps://github.com/sbt/sbt/releases/latest 2> &1 | grep“

正在尝试从GitHub下载最新SBT版本:

version=“$(curl-vsLkhttps://github.com/sbt/sbt/releases/latest 2> &1 | grep“

version
设置为
v1.1.0-RC2

然后尝试下载.tar.gz包:

curl-fsSLk”https://github.com/sbt/sbt/archive/${version}.tar.gz“| tar xvfz--C/home/myuser

但是,不是正确的URL:

https://github.com/sbt/sbt/archive/v1.1.0-RC2.tar.gz

不知何故,版本字符串被解释为命令(?!),导致:

.tar。gzttps://github.com/sbt/sbt/archive/v1.1.0-RC2

当我手动设置
version=“v1.1.0-RC2”
时,这不会发生


提前谢谢

您应该在
curl
命令中使用
-I
标志和一个更简单的管道来获取如下版本号:

curl -sILk https://github.com/sbt/sbt/releases/latest |
awk -F '[/ ]+' '$1 == "Location:"{sub(/\r$/, ""); print $NF}'

v1.1.0-RC2
另请注意使用
sub
功能从
curl
输出的行尾剥离
\r

您的脚本:

version=$(curl -sILk https://github.com/sbt/sbt/releases/latest | awk -F '[/ ]+' '$1 == "Location:"{sub(/\r$/, ""); print $NF}')

curl -fsSLk "https://github.com/sbt/sbt/archive/${version}.tar.gz" | tar xvfz - -C /home/myuser

它绝对是字符串的一部分--它只是一个包含一个序列的字符串,该序列将光标发送回行的前面,因此当它被打印时,它看起来像是在覆盖命令。这并不意味着这样的事情正在发生。谢谢@BenjaminW。!在末尾添加一个`| sed's/\r//g``修复了它。顺便说一句,这实际上是中“询问问题代码之前”部分的第一件事。