操作系统比对脚本[inststuff.pl](转)[@more@]
#!/usr/bin/perl -w
###########################################################################
# Global definitions
###########################################################################
use Getopt::Long;
$rsync="/usr/local/bin/rsync";
$ssh="/usr/local/bin/ssh";
$rsync_cmd="$rsync -Rapzq --partial -e $ssh";
###########################################################################
# Check for help request. Display help page if --help/-h/-? was passed.
###########################################################################
sub help_page {
my $programname = $0;
$programname =~ s#^./##;
print "$programname - . ";
print " Usage: $programname ";
exit 1;
}
sub GetParas
{
my ($argv);
$argv = join(,@ARGV);
$argv = join(,@ARGV);
&help_page if ( $argv eq "" || $argv =~ m!-h! );
GetOptions("d:s");
if ($opt_d eq "")
{
&help_page;
exit(1);
}
return ($opt_d);
}
sub Env{
if ($> != 0)
{
print (" The command need root permission! ");
exit(1);
}
}
###########################################################################
#Main routine
###########################################################################
sub main
{
my $help = grep(/^--help$/, @ARGV) || grep (/^-h$/, @ARGV) || grep (/^-?$/, @ARGV) || 0;
help_page() if $help;
#check user uid
&Env;
my $dirs=&GetParas;
#remove file from localhost
if (-e "$dirs/delfilelist")
{
foreach $removefile (`cat $dirs/delfilelist`)
{
chomp($removefile);
if (-e "$removefile")
{
print "---remove---$removefile ";
system("$rsync_cmd "$removefile" $dirs/del/");
system("mv "$removefile" "$dirs/del$removefile" 2>/dev/null");
}
}
}
#copy newfile to localhost
if (-e "$dirs/addfilelist")
{
foreach $filename (`cat $dirs/addfilelist`)
{
chomp($filename);
chdir("$dirs/filedir");
if(! $filename eq "")
{
print "---add---$filename ";
system("$rsync_cmd .$filename /");
}
}
}
#update localhost file
if (-e "$dirs/updatefilelist")
{
foreach $updatefile (`cat $dirs/updatefilelist`)
{
chomp($updatefile);
chdir("$dirs/filedir");
if ( -e "$updatefile" )
{
print "---update---$updatefile ";
system("$rsync_cmd .$updatefile /");
}
}
}
}
&main
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8225414/viewspace-965597/,如需转载,请注明出处,否则将追究法律责任。