从CURL grep调用中获得第一个匹配项

时间:2019-05-02 09:04:48

标签: bash shell curl

目标:

我正在尝试编写一个脚本,该脚本将从GitHub发布页面获取两个URL,并对每个URL进行不同的处理。

到目前为止:

这是我到目前为止所拥有的。

λ curl -s https://api.github.com/repos/mozilla-iot/gateway/releases/latest | grep "browser_download_url.*tar.gz" | cut -d : -f 2,3 | tr -d \"

这将返回以下内容:

"https://github.com/mozilla-iot/gateway/releases/download/0.8.1/gateway-8c29257704ddb021344bdaaa790909a0eacf3293bab94e02859828a6fd9b900a.tar.gz"
"https://github.com/mozilla-iot/gateway/releases/download/0.8.1/node_modules-921bd0d58022aac43f442647324b8b58ec5fdb4df57a760e1fc81a71627f526e.tar.gz"

我希望能够创建一些目录,拉入第一个目录,解压缩后从新拉出的zip中导航目录,然后拉入第二个目录。

2 个答案:

答案 0 :(得分:1)

通过将输出管道输送到head -n1可以很容易地获取第一行。为了解决您的问题,您不仅需要获取cURL输出的第一个URL。试试看:

#!/bin/bash

# fetch your URLs
answer=`curl -s https://api.github.com/repos/mozilla-iot/gateway/releases/latest | grep "browser_download_url.*tar.gz" | cut -d : -f 2,3 | tr -d \"`

# get URLs and file names
first_file=`echo "$answer" | grep -Eo '.+?\.tar\.gz' | head -n1 | tr -d " "`
second_file=`echo "$answer" | grep -Eo '.+?\.tar\.gz' | head -n2 | tail -1 | tr -d " "`
first_file_name=`echo "$answer" | grep -Eo '[^/]+?\.tar\.gz' | head -n1 `
second_file_name=`echo "$answer" | grep -Eo '[^/]+?\.tar\.gz' | head -n2 | tail -1`

#echo $first_file
#echo $first_file_name
#echo $second_file_name
#echo $second_file

# download first file
wget "$first_file"

# extracting first one that must be in the current directory.
# else, change the directory first and put the path before $first_file!
tar -xzf "$first_file_name"

# do your stuff with the second file

答案 1 :(得分:0)

您可以简单地将URL传递到xargs curl;

curl -s https://api.github.com/repos/mozilla-iot/gateway/releases/latest |
grep "browser_download_url.*tar.gz" |
cut -d : -f 2,3 | tr -d \" |
xargs curl -O

或者,如果您想对每个URL进行更多操作,则可以遍历结果:

curl ... | grep ... | cut ... | tr ... |
while IFS= read -r url; do
    curl -O "$url"
    : maybe do things with "$url" here
done

后者可以很容易地扩展为类似

... | while IFS= read -r url; do
    d=${url##*/}
    mkdir -p "$d"
    ( cd "$d"
      curl -O "$url" 
      tar zxf *.tar.gz
      # end of subshell means effects of "cd" end
    )
 done