-
前提:从github上下载一个人所有仓库,仓库数量93个,要是手动下,呵呵
-
想从网上找现成的轮子,发现有一个2013年的博客https://blog.csdn.net/kkme88/article/details/12651065,里面的脚本已经不适用现在的网页结构
-
刚好学习Linux shell,自己动手写一个
-
过程难点
-
正则提取下载链接,在我的一篇博客中,Java正则表达式,写了一些正则表达式的用法,在最后的例子中,有获取<p></p>标签中间内容的正则,可以用来获取网页a标签中href内容
-
github上仓库的链接就是以下,需要提取 href=" 和 "> 之间的内容
<a itemprop="name codeRepository" data-hovercard-type="repository" data-hovercard-url="/opencv/opencv/hovercard" href="/opencv/opencv">
-
获取分页数,我采用的方法是提取page=后的数字,然后排序,最大值就是分页数
<a rel="next" href="/spring?page=2">2</a> <a class="next_page" rel="next" href="/spring?page=2">Next</a></div>
-
-
脚本代码
#!/bin/bash git_host="https://github.com" tmp_log="tmp_repos.log" function gitclone() { username=$1 if [ ! -d $username ];then mkdir -p $username fi cd $username if [ -f $tmp_log ];then rm $tmp_log fi # 查找分页数,发现两种,一种找到总仓库数,再得到页数,另一种是直接找到最大的页码,这里选择第二种 curl $git_host/$username?tab=repositories | grep page= | grep -oP '(?<=href=").*?(?=">)'|sort > $tmp_log pageNum=$(grep 'page=' $tmp_log | tail -n 1 | grep -oP '[0-9]?') echo $pageNum rm $tmp_log #是否分页 if [ ! $pageNum ];then curl $git_host/$username?tab=repositories | grep codeRepository | grep -oP '(?<=href=").*?(?=")' > $tmp_log else for ((i=1;i<=$pageNum;i++)) do curl "$git_host/$username?tab=repositories&page=$i" | grep codeRepository | grep -oP '(?<=href=").*?(?=")' >> $tmp_log done fi cat $tmp_log | while read line do # 找到仓库的名字 name=$(basename $line) # 方式一:下载zip包 wget https://codeload.github.com$line/zip/master -O $name.zip # 方式二:git clone # git clone https://github.com$line.git done rm $tmp_log } gitclone $1
Linux shell案例:获取github上一个人所有仓库
最新推荐文章于 2023-08-21 09:41:36 发布