0

我想编写一个脚本来查找镜像中可用的给定软件包的最新版本 rpm,例如: http: //mirror.centos.org/centos/5/updates/x86_64/RPMS/

该脚本应该能够在大多数 linux 风格(例如 centos、redhat、ubuntu)上运行。所以基于 yum 的解决方案不是一个选择。是否有任何现有的脚本可以做到这一点?或者有人可以给我一个关于如何解决这个问题的一般想法吗?

4

3 回答 3

1

使用 wget 和 gawk

#!/bin/bash
pkg="kernel-headers"
wget -O- -q http://mirror.centos.org/centos/5/updates/x86_64/RPMS | awk -vpkg="$pkg" 'BEGIN{
    RS="\n";FS="</a>"
    z=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",D,"|")
    for(i=1;i<=z;i++){
       date[D[i]]=sprintf("%02d",i)
    }
    temp=0
}
$1~pkg{
    p=$1
    t=$2
    gsub(/.*href=\042/,"",p)
    gsub(/\042>.*/,"",p)
    m=split(t,timestamp," ")
    n=split(timestamp[1],d,"-")
    q=split(timestamp[2],hm,":")
    datetime=d[3]date[d[2]]d[1]hm[1]hm[2]
    if ( datetime >= temp ){
        temp=datetime
        filepkg = p
    }
}
END{
    print "Latest package: "filepkg", date: ",temp
}'

上面的示例运行:

linux$ ./findlatest.sh
Latest package: kernel-headers-2.6.18-164.6.1.el5.x86_64.rpm, date:  200911041457
于 2009-12-10T14:08:54.850 回答
1

感谢 levislevis85 获取 wget cli。试试这个:

ARCH="i386"
PKG="pidgin-devel"
URL=http://mirror.centos.org/centos/5/updates/x86_64/RPMS
DL=`wget -O- -q $URL | sed -n 's/.*rpm.>\('$PKG'.*'$ARCH'.rpm\).*/\1/p' | sort | tail -1`
wget $URL/$DL

我会把我的评论放在这里,否则代码将无法阅读。

试试这个:

ARCH="i386"
PKG="pidgin-devel"
URL=http://mirror.centos.org/centos/5/updates/x86_64/RPMS
DL=`wget -O- -q $URL | sed -n 's/.*rpm.>\('$PKG'.*'$ARCH'.rpm\).*<td align="right">\(.*\)-\(.*\)-\(.*\) \(..\):\(..\)  <\/td><td.*/\4 \3 \2 \5 \6 \1/p' | sort -k1n -k2M -k3n -k4n -k5n | cut -d ' ' -f 6 | tail -1`
wget $URL/$DL

它的作用是:
wget - 获取索引文件
sed - 剪下一些部分并以不同的顺序将它们放在一起。应产生年月日时分和包,如:

2009 Oct 27 01 14 pidgin-devel-2.6.2-2.el5.i386.rpm
2009 Oct 30 10 49 pidgin-devel-2.6.3-2.el5.i386.rpm

排序 - 为数字排序 n 列,为月份排序 M
剪切 - 删除归档的 6
尾 - 仅显示最后一个条目

the problem with this could be, if some older package release comes after a newer then this script will also fail. If the output of the site changes, the script will fail. There are always a lot of points where a script could fail.

于 2009-12-10T17:16:51.257 回答
0

试试这个(需要lynx):

lynx -dump -listonly -nonumbers http://mirror.centos.org/centos/5/updates/x86_64/RPMS/ |
    grep -E '^.*xen-libs.*i386.rpm$' |
    sort --version-sort |
    tail -n 1

如果您sort没有--version-sort,那么您将不得不从文件名中解析版本,或者希望常规排序会做正确的事情。

您可以wget使用curl带有/dev/tcp/HOST/PORT. 这些问题是您必须解析 HTML

于 2009-12-10T06:26:19.540 回答