Freebsd精简系统的ldd提取文件的perl脚本

在做freebsd精简版系统时,要从原系统用 ldd 命令提取相关文件到精简的安放目录去,但由于本人老眼昏花,所以都要浪费N多精力注册屏幕细致的 copy拷贝复制文件,觉得好烦,而且手工容易出错,想,如果用脚本解决copy文件就好了。

好早的时间之前,曾经搞过Linux的精简研究,那时从网上找来了可以自己 ldd 提取文件的perl脚本,所以现在尝试在Freebsd系统下应用,当然,实验成功,这以后提取文件作Freebsd精简版系统不用这么烦了,不过不能提取配置文件,但库文件的提取就没问题了,以后Freebsd精简爽了 !

例子,提取php命令和相关库文件:

# ./cpfile /usr/local/bin/php -t /root/tmp

/root/tmp是我的安放文件目录,php好像还有配置文件的,这个要手工。

以下是perl脚本

perl脚本cpfile.pl

————————————————

#!/usr/bin/perl
#===============================================================================
#
# DESCRIPTION: Get library
#
# AUTHOR: DawnFantasy, <goldenshore999 # gmail # com>
# VERSION: 1.1
# DATE: 2008-02-04
# Record: https://www.lpfrx.com
#===============================================================================
use strict;
use warnings;
use Getopt::Long;
my %opts;
$opts{target} = ‘initrd’;
usage(), exit -1
if ( not GetOptions( \%opts, ‘import=s’, ‘verbose’, ‘target|t=s’ ) );
usage(), exit -1
if ( ( not @ARGV ) xor ( defined $opts{import} ) );
sub usage {
print <<EOU;
Usage:
$0 file1 [file2…fileX] [-t DIR] [-v]
or
$0 -i list [-t DIR] [-v]
Options:
-i, –import=FILE Process files listed in FILE
-t, –target=DIR Use DIR as root. (Default ./initrd)
-v, –verbose Be verbose.
EOU
return;
}

## files to process
my @files;
{
my @filess;
{
## files in @ARGV
if ( not defined $opts{import} ) {
push @filess, @ARGV;
last;
}
## import list
open my $fh, ‘<‘, $opts{import}
or die “$!\n”;
@filess = (<$fh>);
@filess = map { chomp; $_ } @filess;
close $fh;
}
print “File to process: (! means file NOT found.)\n”;
foreach (@filess) {
print ” ! $_ \n” and next if not -f $_;
push @files, $_;
print ” $_\n”;
}
}
## parse libs to copy
my @libs;
{
print “\n”;
foreach (@files) {
my $ldd = qx{/usr/bin/ldd $_};
chomp $ldd;
print “$ldd\n\n” if defined $opts{verbose};
my @files = split /\s+/, $ldd;
push @libs, grep { -f $_ } @files; ## check for existence
}
## Unified them
my %h = map { ( $_, 1 ) } @libs;
@libs = sort keys %h;
if ( defined $opts{verbose} ) {
print “Libraries to copy:\n”;
print ” $_ => $opts{target}$_\n” foreach @libs;
}
}
## Do the real work
print “\n”;
{
print “Copying files, please wait…\n”;
## Add those files in the original list
push @libs, @files;
if ( not -d $opts{target} ) {
print “Creating directory $opts{target}\n”;
system qq{ mkdir -p $opts{target} };
}
my $v = $opts{verbose} ? ‘v’ : ”;
foreach (@libs) {
if (/^\//) {
## 绝对目录
s/^\///;
system
qq{ tar -C / -hpmcf – $_ | tar -C $opts{target} -pmx$v -f – };
} else {
## 相对目录
system
qq{ tar -hpmcf – $_ | tar -C $opts{target} -pmx$v -f – };
}
}
}

以下是文件链接:
cpfile

关于无聊人

一个无聊人而已
此条目发表在perl分类目录。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注