1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#!/usr/bin/perl
package NEWEST;
use Getopt::Long;
use File::Basename;
my $src_dir;
my $basename;
my $type = "tar.gz";
my $versions;
my $help;
my %KEEPER;
GetOptions(
"src_dir=s" => \$src_dir,
"basename=s" => \$basename,
"type=s" => \$type,
"versions!" => \$versions,
"help!" => \$help
);
if (!defined $src_dir || !defined $basename) {
$help = 1;
}
if ($help) {
&help();
exit;
}
&extract_version(\$src_dir, \$basename, \$type, \%KEEPER);
&print_max(\%KEEPER, \$type, \$versions, &find_max(\%KEEPER));
sub extract_version {
my $src_dir = shift;
my $basename = shift;
my $type = shift;
my $KEEPER = shift;
while (glob("$${src_dir}/$${basename}*")) {
my $base = basename("$_",".$${type}");
my @ver = split /-/, $base;
my @nums = split /\./, $ver[$#ver];
my $new;
for (my $i=0; $i<$#nums+1; $i++) {
$new =~ s/^([0-9]*)([a-zA-Z]*)$/$1/;
$new .= 10000+$nums[$i];
$new .= $2;
}
$KEEPER->{"$new"} = [$base,$ver[$#ver]];
}
return;
}
sub find_max {
my $KEEPER = shift;
return reverse sort (keys %$KEEPER);
}
sub print_max {
my $KEEPER = shift;
my $type = shift;
my $versions = shift;
my $max_key = shift;
if ($${versions}) {
print "$KEEPER->{$max_key}->[1]\n";
}
else {
print "$KEEPER->{$max_key}->[0]" . ".$${type}\n";
}
return;
}
sub help {
print qq("newest" finds the tarball in a given directory with the newest version number
and returns it's filename. "newest" is meant to be embedded in UNIX shell
scripts.
Usage:
newest -(src_dir | s) /path/to/dir/with/tarballs
-(basename | b) BaseName (ex. BaseName-2.10.tar.gz)
-(type | t) Type of file (default: tar.gz)
-(versions | v) Print only version information
-(help | h) Prints usage help
Ex: \$ /opt/bin/newest -s /opt/incoming/pm_modules -b Data-Dumper
Data-Dumper-2.101.tar.gz
Both arguments, '-s' and '-b' are required; '-t' and '-v' are optional.
);
return;
}
|