English 中文(简体)
find latest version of rpms from a mirror
原标题:
  • 时间:2009-12-10 02:05:01
  •  标签:
  • shell
  • rpm

I want to write a script to find the latest version of rpm of a given package available from a mirror for eg: http://mirror.centos.org/centos/5/updates/x86_64/RPMS/

The script should be able to run on majority of linux flavors (eg centos, redhat, ubuntu). So yum based solution is not an option. Is there any existing script that does this? Or can someone give me a general idea on how to go about this?

最佳回答

Thx to levislevis85 for the wget cli. Try this:

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

I Will put my comment here, otherwise the code will not be readable.

Try this:

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

What it does is:
wget - get the index file
sed - cut out some parts and put it together in different order. Should result in Year Month Day Hour Minute and Package, like:

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

sort - order the columns n stays for numerical and M for month
cut - cut out the filed 6
tail - show only last entry

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.

问题回答

using wget and gawk

#!/bin/bash
pkg="kernel-headers"
wget -O- -q http://mirror.centos.org/centos/5/updates/x86_64/RPMS | awk -vpkg="$pkg"  BEGIN{
    RS="
";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=42/,"",p)
    gsub(/42>.*/,"",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
} 

an example run of the above:

linux$ ./findlatest.sh
Latest package: kernel-headers-2.6.18-164.6.1.el5.x86_64.rpm, date:  200911041457

Try this (which requires 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

If your sort doesn t have --version-sort, then you ll have to parse the version out of the filename or hope that a regular sort will do the right thing.

You may be able to do something similar with wget or curl or even a Bash script using redirections with /dev/tcp/HOST/PORT. The problem with these is that you would then have to parse HTML.





相关问题
KornShell- Creating a fixed width text file

I need to create a simple fixed width text file in KornShell (ksh). My current attempt using printf to pad the string isn t working out very well. What s the shortest, cleanest way to create a fixed ...

unix find command

how to use the find command to find files/directories which are not matching the pattern. for eg: find <some options > -name "dontfile.txt" should give me output of all the find whose file ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

1. 露台式照像

在Windows XP中,没有一时出现指挥时,是否有办法操作一种灰色文字? 我常常需要把“善待”(工作)与“灰色”同起来,即使我的文字没有产出,......

热门标签