以下 shell 语句下载文件:
curl -s https://api.github.com/repos/kubernetes-sigs/kustomize/releases/latest |\
grep browser_download |\
grep $opsys |\
cut -d '"' -f 4 |\
xargs curl -O -L |\
并且我尝试添加附加语句来提取文件:
curl -s https://api.github.com/repos/kubernetes-sigs/kustomize/releases/latest |\
grep browser_download |\
grep $opsys |\
cut -d '"' -f 4 |\
xargs curl -O -L |\
tar xz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 648 100 648 0 0 1674 0 --:--:-- --:--:-- --:--:-- 1670
100 10.9M 100 10.9M 0 0 3863k 0 0:00:02 0:00:02 --:--:-- 6833k
gzip: stdin: unexpected end of file
tar: Child returned status 1
tar: Error is not recoverable: exiting now
但如您所见,我收到了一条错误消息。我做错了什么?
答案1
您可以使用 Perl 类型的正则表达式来简化 grep,并且 xargs 需要 `-d '\n',因为 URL 每行一个:
curl -s https://api.github.com/repos/kubernetes-sigs/kustomize/releases/latest |grep -Po 'browser_download_url"[^"]+"\K[^"]+' |xargs -d '\n' curl -L -O
然后在单独的步骤解压所有压缩文件文件:
tar xf *.tar.gz
如果需要一条语句,可以用 将它们连接在一起&&
。
答案2
该-O
选项意味着
-O, --remote-name Write output to a local file named like the remote file we get. (Only the file part of the remote file is used, the path is cut off.) The file will be saved in the current working directory.
因此,没有任何内容进入到 管道tar
。要curl
改为写入标准输出,请删除该-O
选项。